-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
SIP-62 - For comprehension improvements (#20522)
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 import `scala.language.experimental.betterFors`. The main changes are: 1. **Starting `for` comprehensions with aliases**: - **Current Syntax**: ```scala val a = 1 for { b <- Some(2) c <- doSth(a) } yield b + c ``` - **New Syntax**: ```scala for { a = 1 b <- Some(2) c <- doSth(a) } yield b + c ``` 2. **Simpler Desugaring for Pure Aliases**: - **Current Desugaring**: ```scala for { a <- doSth(arg) b = a } yield a + b ``` Desugars to: ```scala doSth(arg).map { a => val b = a (a, b) }.map { case (a, b) => a + b } ``` - **New Desugaring**: (where possible) ```scala doSth(arg).map { a => val b = a a + b } ``` 3. **Avoiding Redundant `map` Calls**: - **Current Desugaring**: ```scala for { a <- List(1, 2, 3) } yield a ``` Desugars to: ```scala List(1, 2, 3).map(a => a) ``` - **New Desugaring**: ```scala List(1, 2, 3) ```
- Loading branch information
Showing
10 changed files
with
257 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
List((1,3), (1,4), (2,3), (2,4)) | ||
List((1,2,3), (1,2,4)) | ||
List((1,3), (1,4), (2,3), (2,4)) | ||
List((2,3), (2,4)) | ||
List((2,3), (2,4)) | ||
List((1,2), (2,4)) | ||
List(1, 2, 3) | ||
List((2,3,6)) | ||
List(6) | ||
List(3, 6) | ||
List(6) | ||
List(2) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
import scala.language.experimental.betterFors | ||
|
||
def for1 = | ||
for { | ||
a = 1 | ||
b <- List(a, 2) | ||
c <- List(3, 4) | ||
} yield (b, c) | ||
|
||
def for2 = | ||
for | ||
a = 1 | ||
b = 2 | ||
c <- List(3, 4) | ||
yield (a, b, c) | ||
|
||
def for3 = | ||
for { | ||
a = 1 | ||
b <- List(a, 2) | ||
c = 3 | ||
d <- List(c, 4) | ||
} yield (b, d) | ||
|
||
def for4 = | ||
for { | ||
a = 1 | ||
b <- List(a, 2) | ||
if b > 1 | ||
c <- List(3, 4) | ||
} yield (b, c) | ||
|
||
def for5 = | ||
for { | ||
a = 1 | ||
b <- List(a, 2) | ||
c = 3 | ||
if b > 1 | ||
d <- List(c, 4) | ||
} yield (b, d) | ||
|
||
def for6 = | ||
for { | ||
a = 1 | ||
b = 2 | ||
c <- for { | ||
x <- List(a, b) | ||
y = x * 2 | ||
} yield (x, y) | ||
} yield c | ||
|
||
def for7 = | ||
for { | ||
a <- List(1, 2, 3) | ||
} yield a | ||
|
||
def for8 = | ||
for { | ||
a <- List(1, 2) | ||
b = a + 1 | ||
if b > 2 | ||
c = b * 2 | ||
if c < 8 | ||
} yield (a, b, c) | ||
|
||
def for9 = | ||
for { | ||
a <- List(1, 2) | ||
b = a * 2 | ||
if b > 2 | ||
} yield a + b | ||
|
||
def for10 = | ||
for { | ||
a <- List(1, 2) | ||
b = a * 2 | ||
} yield a + b | ||
|
||
def for11 = | ||
for { | ||
a <- List(1, 2) | ||
b = a * 2 | ||
if b > 2 && b % 2 == 0 | ||
} yield a + b | ||
|
||
def for12 = | ||
for { | ||
a <- List(1, 2) | ||
if a > 1 | ||
} yield a | ||
|
||
object Test extends App { | ||
println(for1) | ||
println(for2) | ||
println(for3) | ||
println(for4) | ||
println(for5) | ||
println(for6) | ||
println(for7) | ||
println(for8) | ||
println(for9) | ||
println(for10) | ||
println(for11) | ||
println(for12) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.