I have a question about
if expressions that have no
else clause. It’s generally inappropriate to access the value of such an expression, but beginners make mistakes and the symptoms (incl. consequent error messages) matter.
Here’s some toy code in Scala 2:
val x = 123 val foo = if (x > 0) 1 // type of foo is AnyVal val bar = if (x > 0) 1 else if (x < 0) 2 // type of bar is AnyVal
And the same in Scala 3:
val x = 123 val foo = if x > 0 then 1 // type of foo is Unit(!) val bar = if x > 0 then 1 else if x < 0 then 2 // type of bar is AnyVal
I love Scala 3, but the way Scala 2 treats these expressions seems simpler to explain. Moreover, such code is generally written in error, and Scala 2 produces more consistent symptoms.
I wonder if the change in Scala 3 is intentional and permanent?