Skip to content

Commit be1f5b0

Browse files
committed
Cleanup for experimental SIP-62 implementation
1 parent ba9806b commit be1f5b0

File tree

4 files changed

+45
-3
lines changed

4 files changed

+45
-3
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
@@ -67,7 +67,8 @@ object Feature:
6767
(into, "Allow into modifier on parameter types"),
6868
(namedTuples, "Allow named tuples"),
6969
(modularity, "Enable experimental modularity features"),
70-
(betterMatchTypeExtractors, "Enable better match type extractors")
70+
(betterMatchTypeExtractors, "Enable better match type extractors"),
71+
(betterFors, "Enable improvements in `for` comprehensions")
7172
)
7273

7374
// 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
@@ -127,7 +127,7 @@ object language:
127127

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

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)