I understand the reasoning, but it just bit me when I (by mistake) did exactly that: assigned under the condition instead of above it, and then passed it on to a nillable arg
I think this is a good point.
We copied this behavior from Ruby: when you don’t assign to a variable in all branches it just gets to be
Nil. This is “good” because you don’t need to write
value = nil on the other branch. This is bad because the
nil assignment is implicit so it can’t be seen. An immediate consequence of this is that if you try to invoke something on
value you will get a compile-error but there’s no code to show where did that
nil come from (well, I think the compiler will point to the
if but it’s still not clear). The same happens with a
case only it’s much worse: with such compiler error you have to check all branches to see where did you make a mistake.
Maybe we can remove this behavior? Then you will get a compile error when you forget to assign a variable in one branch, removing the “nil if not assigned” behavior.
This would make code easier to read and follow at the cost of having to type a bit more. I really don’t mind that.
I wonder how much code will break because of this. Maybe not that much, I don’t know…