4 ms·
Because it announces that you intend to use the name 'A' in the current scope after some assignment happens in a nested one. It's intentional. It /can/ be usef
by haimez 10y ago
Because it announces that you intend to use the name 'A' in the current scope after some assignment happens in a nested one. It's intentional.
It /can/ be useful, but it should be opt-in. JavaScript had to figure this out with their 'var' declaration scoping too.
Also, try:
A = (predicate) ? 3 : 5
Or, in a language that embraces expressions instead of statements:
val A = if (predicate) { 3 } else { 5 }
These are all more intentional, concise, and readable than allowing variables to outlive their scope by default.
- ucho 10y agoJava has also alternative approach: final a; if (predicate) { a = 3; } else { a = 5; } `final` makes it possible for variable to be assigned exactly once. Verbose but useful when "?:" is not a good fit.