Skip to content

Treat all incomplete ifs as statements #14960

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

Merged
merged 2 commits into from
Apr 19, 2022
Merged

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