Skip to content

Commit 6ef7d8e

Browse files
committed
Cleanup for experimental SIP-62 implementation
1 parent 9c3e454 commit 6ef7d8e

File tree

5 files changed

+47
-4
lines changed

5 files changed

+47
-4
lines changed

compiler/src/dotty/tools/dotc/ast/Desugar.scala

+41-1
Original file line numberDiff line numberDiff line change
@@ -1804,7 +1804,7 @@ object desugar {
18041804
/** Create tree for for-comprehension `<for (enums) do body>` or
18051805
* `<for (enums) yield body>` where mapName and flatMapName are chosen
18061806
* corresponding to whether this is a for-do or a for-yield.
1807-
* The creation performs the following rewrite rules:
1807+
* If betterFors are enabled, the creation performs the following rewrite rules:
18081808
*
18091809
* 1.
18101810
*
@@ -1872,6 +1872,46 @@ object desugar {
18721872
* (Where empty for-comprehensions are excluded by the parser)
18731873
*
18741874
* If the aliases are not followed by a guard, otherwise an error.
1875+
*
1876+
* With betterFors disabled, the translation is as follows:
1877+
*
1878+
* 1.
1879+
*
1880+
* for (P <- G) E ==> G.foreach (P => E)
1881+
*
1882+
* Here and in the following (P => E) is interpreted as the function (P => E)
1883+
* if P is a variable pattern and as the partial function { case P => E } otherwise.
1884+
*
1885+
* 2.
1886+
*
1887+
* for (P <- G) yield E ==> G.map (P => E)
1888+
*
1889+
* 3.
1890+
*
1891+
* for (P_1 <- G_1; P_2 <- G_2; ...) ...
1892+
* ==>
1893+
* G_1.flatMap (P_1 => for (P_2 <- G_2; ...) ...)
1894+
*
1895+
* 4.
1896+
*
1897+
* for (P <- G; E; ...) ...
1898+
* =>
1899+
* for (P <- G.filter (P => E); ...) ...
1900+
*
1901+
* 5. For any N:
1902+
*
1903+
* for (P_1 <- G; P_2 = E_2; val P_N = E_N; ...)
1904+
* ==>
1905+
* for (TupleN(P_1, P_2, ... P_N) <-
1906+
* for (x_1 @ P_1 <- G) yield {
1907+
* val x_2 @ P_2 = E_2
1908+
* ...
1909+
* val x_N & P_N = E_N
1910+
* TupleN(x_1, ..., x_N)
1911+
* } ...)
1912+
*
1913+
* If any of the P_i are variable patterns, the corresponding `x_i @ P_i` is not generated
1914+
* and the variable constituting P_i is used instead of x_i
18751915
*
18761916
* @param mapName The name to be used for maps (either map or foreach)
18771917
* @param flatMapName The name to be used for flatMaps (either flatMap or foreach)

compiler/src/dotty/tools/dotc/config/Feature.scala

+2-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,8 @@ object Feature:
6868
(into, "Allow into modifier on parameter types"),
6969
(namedTuples, "Allow named tuples"),
7070
(modularity, "Enable experimental modularity features"),
71-
(betterMatchTypeExtractors, "Enable better match type extractors")
71+
(betterMatchTypeExtractors, "Enable better match type extractors"),
72+
(betterFors, "Enable improvements in `for` comprehensions")
7273
)
7374

7475
// legacy language features from Scala 2 that are no longer supported.

library/src/scala/runtime/stdLibPatches/language.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ object language:
135135

136136
/** Experimental support for improvements in `for` comprehensions
137137
*
138-
* @see [[https://dotty.epfl.ch/docs/reference/experimental/better-fors]]
138+
* @see [[https://github.com/scala/improvement-proposals/pull/79]]
139139
*/
140140
@compileTimeOnly("`betterFors` can only be used at compile time in import statements")
141141
object betterFors

project/MiMaFilters.scala

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ object MiMaFilters {
88
val ForwardsBreakingChanges: Map[String, Seq[ProblemFilter]] = Map(
99
// Additions that require a new minor version of the library
1010
Build.mimaPreviousDottyVersion -> Seq(
11-
11+
ProblemFilters.exclude[MissingFieldProblem]("scala.runtime.stdLibPatches.language#experimental.betterFors"),
12+
ProblemFilters.exclude[MissingClassProblem]("scala.runtime.stdLibPatches.language$experimental$betterFors$"),
1213
),
1314

1415
// Additions since last LTS

tests/run/fors.scala

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
import annotation.tailrec
88

9+
@scala.annotation.experimental
910
object Test extends App {
1011
val xs = List(1, 2, 3)
1112
val ys = List(Symbol("a"), Symbol("b"), Symbol("c"))

0 commit comments

Comments
 (0)