Closed
Description
Compiler version
3.1.1
Minimized code
This code prints ()
, as a
evaluates to the Unit
value:
val a =
if false then
1
else if true then
2
println(a)
Update: this code may be a bit too minimized to demonstrate the scope of the actual problem. See the examples in this comment for more clarity.
Output
()
Expectation
Although this code is unusual and unlikely to be written by an experienced programmer (it is a simplified and minimized version of something written by my 8-year-old son who has just started learning to program with Scala), I suspect most people would intuitively expect a
to evaluate to 2
rather than to ()
.
Adding an unconditional else
clause to the if
statement restores expected behaviour:
val a =
if false then
1
else if true then
2
else
3
println(a) // prints 2