You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/_spec/06-expressions.md
+5-4
Original file line number
Diff line number
Diff line change
@@ -729,16 +729,17 @@ A _for loop_ `for (´\mathit{enums}\,´) ´e´` executes expression ´e´ for ea
729
729
A _for comprehension_`for (´\mathit{enums}\,´) yield ´e´` evaluates expression ´e´ for each binding generated by the enumerators ´\mathit{enums}´ and collects the results.
730
730
An enumerator sequence always starts with a generator; this can be followed by further generators, value definitions, or guards.
731
731
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.
734
735
735
736
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´.
736
737
A _guard_`if ´e´` contains a boolean expression which restricts enumerated bindings.
737
738
The precise meaning of generators and guards is defined by translation to invocations of four methods: `map`, `withFilter`, `flatMap`, and `foreach`.
738
739
These methods can be implemented in different ways for different carrier types.
739
740
740
741
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
742
743
743
744
```scala
744
745
´p´ <- ´e´.withFilter { case ´p´ =>true; case _ =>false }
@@ -772,7 +773,7 @@ Then, the following rules are applied repeatedly until all comprehensions have b
-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\,´ })`.
776
777
777
778
-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:
0 commit comments