-
Notifications
You must be signed in to change notification settings - Fork 1.1k
‘if … else if’ expression unexpectedly has Unit value when last branch taken #14914
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
I'm aware that this is as designed. Scala 2 would append Another context was whether Scala 2 Turning it around, maybe the wart is consuming an You're never too young to learn to minimize Scala. |
Duplicates #12890 |
@som-snytt Thank you for the comments and link to the related issue. The discussion on #12890 is specifically about the case of an If this (frankly weird and counter-intuitive) behaviour is really intended even with multiple branches, could we at least improve the warning message? I didn’t find the existing message ( Thank you for your consideration. |
Your question did make me think that an alternative behavior would be to pick a result type based on the existing legs, then add the "default" leg using the "default value" for the type, which is |
@som-snytt :-) The situation is perhaps more subtle than may have thus far been realized, which is why it is so confusing (and it took me a while to figure out what was going on in my son’s code). It is not simply the case that these Thus: val x = "1"
val a =
if x == "1" then
1
else if x == "2" then
2
else if x == "3" then
3
println(a) // prints 1 I’m not even seeing a compiler warning with the above code to give me a hint that there is a problem (at least, not on Scastie). And, likewise: val x = "2"
val a =
if x == "1" then
1
else if x == "2" then
2
else if x == "3" then
3
println(a) // prints 2 Again, there is no compiler warning for the above. However: val x = "3"
val a =
if x == "1" then
1
else if x == "2" then
2
else if x == "3" then
3
println(a) // prints () At least this one gives the compiler warning. I am struggling to see a justification (from a Scala user’s perspective) for why the first two examples here yield a sane (i.e. non- |
Oh my, you were right that it is just one-legged if that is special-cased for value discard. I would guess the
|
(Note that I’d be fine with the |
That's pretty funky. Just the penultimate branch (before the missing else) gets its value discarded (as I now see you said). That must be a glitch.
|
@som-snytt Like you, I would use a |
Agreed! Could we re-open this issue then, please? |
@KacperFKorban hi, if you agree, would you mind reopening? My previous judgment may have been premature. Or, we could start a discussion in discussions. |
@djneades That's a great story. You're never too young to learn to distrust computers. |
Indeed! It’s a good lesson to learn that the compiler can be wrong (or, at least, weird). Ideally, though, that lesson would have come a little later in his programming experience :-) |
Previously only a one-armed if was treated as a statement where all parts were typed with Unit as expected type. We now extend that treatment also to multi-branch ifs that lack a final part. Fixes scala#14914
Previously only a one-armed if was treated as a statement where all parts were typed with Unit as expected type. We now extend that treatment also to multi-branch ifs that lack a final part. Fixes scala#14914
Uh oh!
There was an error while loading. Please reload this page.
Compiler version
3.1.1
Minimized code
This code prints
()
, asa
evaluates to theUnit
value: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 to2
rather than to()
.Adding an unconditional
else
clause to theif
statement restores expected behaviour:The text was updated successfully, but these errors were encountered: