-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
SIP-62 - For comprehension improvements #20522
Conversation
be1f5b0
to
08a8457
Compare
@KacperFKorban Looks reasonable to me. @odersky could you recommend someone to review the code from the dotty side of things? |
Can I now write a for comprehension with only aliases, no generators? |
No, the for-comprehension will still have to contain at least one generator to be valid. |
Maybe it's out of scope, but I think there could be value in lifting that restriction. That would allow aaa x y = let r = 3
s = 6
in r*x + s*y Current Scala: def aaa(x: Double, y: Double) = {
val r = 3
val s = 6
r*x + s*y
} With generator-less def aaa(x: Double, y: Double) =
for {
r = 3
s = 6
}
yield r*x + s*y Perhaps it would be more similar to let...in (and perhaps more worthwhile) with braceless syntax, than those snippets. |
@nafg I think that it is out of scope for this SIP. I also think that reusing the |
Sorry it took so long to review. I think we are close, let's just refactor to keep it DRY. |
4b67eb0
to
d51b7c0
Compare
- [] Avoid redundant map call if the yielded value is the same as the last result. This makes for expressions more efficient and provides more opportunities for tail recursion.
- Allow `for`-comprehensions to start with aliases desugaring them into valdefs in a new block - Desugar aliases into simple valdefs, instead of patterns when they are not followed by a guard - Add an experimental language flag that enables the new desugaring method
d51b7c0
to
4bc0a4a
Compare
@odersky Thanks for the review, it should be closer to DRY now. See if there is anything else I should correct. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks all good now.
Are there any docs for this feature? |
Hmmm, I don't think so. Should I write some? |
Every language level change is meant to come with a spec in the Scala 3 reference |
Yes, it would be good if you could write a spec page for https://docs.scala-lang.org/scala3/reference/. These pages are in |
right now it still has the experimental flag, was it approved for stabilisation? |
It wasn't approved yet, IIRC one of the blockers was that we wanted to run the community build with it turned on by default to see if anything breaks. @WojciechMazur is that something you can help with? |
I've started the build, I'll be back later with the results |
There is a plenty of projects that start to fail with this experimental feature enabled. Most of issues seems to be related to type inference. @KacperFKorban Can I leave it here and let you create a dedicated issues/prs with proper minimization?
I've also found 4 projects that fail to build with only |
@WojciechMazur I have that issue with the -experimental flag at work, discussed here: #20730 |
@WojciechMazur Thanks! Regarding the |
Summary of my findings:
I think that all these issues are one and the same, I created an issue for it: #21804 |
Implementation for SIP-62.
Summary of the changes
For more details read the committed markdown file here: scala/improvement-proposals#79
This introduces improvements to
for
comprehensions in Scala to improve usability and simplify desugaring. The changes are hidden behind a language importscala.language.experimental.betterFors
.The main changes are:
Starting
for
comprehensions with aliases:Simpler Desugaring for Pure Aliases:
Avoiding Redundant
map
Calls: