Skip to content

Conversation

@odersky
Copy link
Contributor

@odersky odersky commented Apr 18, 2022

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 #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
@odersky
Copy link
Contributor Author

odersky commented Apr 18, 2022

The reason for the Scala 2 -> to Scala 3 change was that people found it surprising that

if ... then f.write(x)

would have type AnyVal and not Unit. This can mess up things if the statement is used as a building block for custom DSLs, say. I agree that this was a wart that needed fixing. But the fix introduced another surprise in that multi-branch ifs with
a missing final part now behaved differently. That's fixed by this PR.

@som-snytt
Copy link
Contributor

Thanks! I wonder if there is a better term for one-armed or one-legged, to mean else-less. The structure can be multi-limbed. Maybe biology has a term for when an octopus loses an appendage, or when a salamander loses a tail.

@dwijnand dwijnand merged commit d8c15c8 into scala:main Apr 19, 2022
@dwijnand dwijnand deleted the fix-14914 branch April 19, 2022 07:02
@Kordyjan Kordyjan added this to the 3.2.0 milestone Aug 1, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

‘if … else if’ expression unexpectedly has Unit value when last branch taken

4 participants