Skip to content

Commit 867fb35

Browse files
committed
docs and comments
1 parent 1807f31 commit 867fb35

File tree

2 files changed

+8
-1
lines changed

2 files changed

+8
-1
lines changed

compiler/src/dotty/tools/dotc/transform/localopt/DropForMap.scala

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,13 @@ import dotty.tools.dotc.core.Types.*
1010
import dotty.tools.dotc.transform.MegaPhase.MiniPhase
1111
import dotty.tools.dotc.ast.desugar
1212

13+
/** Drop unused trailing map calls in for comprehensions.
14+
* We can drop the map call if:
15+
* - it won't change the type of the expression, and
16+
* - the function is an identity function or a const function to unit.
17+
*
18+
* The latter condition is checked in [[Desugar.scala#makeFor]]
19+
*/
1320
class DropForMap extends MiniPhase:
1421
import DropForMap.*
1522

docs/_docs/reference/experimental/better-fors.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ Additionally this extension changes the way `for`-comprehensions are desugared.
6060
This change makes the desugaring more intuitive and avoids unnecessary `map` calls, when an alias is not followed by a guard.
6161

6262
2. **Avoiding Redundant `map` Calls**:
63-
When the result of the `for`-comprehension is the same expression as the last generator pattern, the desugaring avoids an unnecessary `map` call. but th eequality of the last pattern and the result has to be able to be checked syntactically, so it is either a variable or a tuple of variables.
63+
When the result of the `for`-comprehension is the same expression as the last generator pattern, the desugaring avoids an unnecessary `map` call. But the equality of the last pattern and the result has to be able to be checked syntactically, so it is either a variable or a tuple of variables. There is also a special case for dropping the `map`, if its body is a constant function, that returns `()` (`Unit` constant).
6464
**Current Desugaring**:
6565
```scala
6666
for {

0 commit comments

Comments
 (0)