Skip to content

Commit 127bb7b

Browse files
committed
Fix #20271: Bring for comprehension spec up to date.
Since Scala 3.4, a `withFilter` is generated if and only if the generator has the `case` modifier. If it does not, the pattern must be irrefutable.
1 parent a672e05 commit 127bb7b

File tree

1 file changed

+5
-4
lines changed

1 file changed

+5
-4
lines changed

docs/_spec/06-expressions.md

+5-4
Original file line numberDiff line numberDiff line change
@@ -729,16 +729,17 @@ A _for loop_ `for (´\mathit{enums}\,´) ´e´` executes expression ´e´ for ea
729729
A _for comprehension_ `for (´\mathit{enums}\,´) yield ´e´` evaluates expression ´e´ for each binding generated by the enumerators ´\mathit{enums}´ and collects the results.
730730
An enumerator sequence always starts with a generator; this can be followed by further generators, value definitions, or guards.
731731

732-
A _generator_ `´p´ <- ´e´` produces bindings from an expression ´e´ which is matched in some way against pattern ´p´.
733-
Optionally, `case` can appear in front of a generator pattern, this has no meaning in Scala 2 but will be [required in Scala 3 if `p` is not irrefutable](https://docs.scala-lang.org/scala3/reference/changed-features/pattern-bindings.html).
732+
A _generator_ `´p´ <- ´e´` produces bindings from an expression ´e´ which is deconstructed by the pattern ´p´.
733+
The pattern must be [irrefutable](08-pattern-matching.html#irrefutable-patterns).
734+
A _conditional generator_ `case ´p´ <- ´e´` tests whether elements produced by ´e´ match the pattern and discards the ones that do not match.
734735

735736
A _value definition_ `´p´ = ´e´` binds the value name ´p´ (or several names in a pattern ´p´) to the result of evaluating the expression ´e´.
736737
A _guard_ `if ´e´` contains a boolean expression which restricts enumerated bindings.
737738
The precise meaning of generators and guards is defined by translation to invocations of four methods: `map`, `withFilter`, `flatMap`, and `foreach`.
738739
These methods can be implemented in different ways for different carrier types.
739740

740741
The translation scheme is as follows.
741-
In a first step, every generator `´p´ <- ´e´`, where ´p´ is not [irrefutable](08-pattern-matching.html#patterns) for the type of ´e´ is replaced by
742+
In a first step, every generator `case ´p´ <- ´e´` is replaced by
742743

743744
```scala
744745
´p´ <- ´e´.withFilter { case ´p´ => true; case _ => false }
@@ -772,7 +773,7 @@ Then, the following rules are applied repeatedly until all comprehensions have b
772773
´e´.foreach { case ´p´ => for (´p'´ <- ´e'; ...´) ´e''´ }
773774
```
774775

775-
- A generator `´p´ <- ´e´` followed by a guard `if ´g´` is translated to a single generator `´p´ <- ´e´.withFilter((´x_1, ..., x_n´) => ´g\,´)` where ´x_1, ..., x_n´ are the free variables of ´p´.
776+
- A generator `´p´ <- ´e´` followed by a guard `if ´g´` is translated to a single generator `´p´ <- ´e´.withFilter({ case ´p´ => ´g\,´ })`.
776777

777778
- A generator `´p´ <- ´e´` followed by a value definition `´p'´ = ´e'´` is translated to the following generator of pairs of values, where ´x´ and ´x'´ are fresh names:
778779

0 commit comments

Comments
 (0)