main :: proc() {
a: int
foo: Foo = 5
ok: bool
a, ok = foo.(int)
}
I understand that separating the declaration and initialization of ok feels a bit clunky. But adding an Odin feature to make this more ergonomic may actually make it more complicated and make the code harder to reason about. Take for example how Go lang handles the error variables:
Also, instead of the name ok, you could name it like ok_foo or ok_foo_int. This is useful when you need to use the name ok for several different types of errors in the same scope.
When checking multiple ok values in the same scope, I prefer giving them unique names (e.g., ok, ok2) so I can keep using :=. Other approaches feel a bit too verbose for my liking.
some_val, ok := some_proc()
if !ok // handle the error
another_val, ok2 := some_proc_2()
if !ok2 // handle the error
But if I don’t want to log anything and just return early with default value if !ok then I use or_return