Skip to content

Commit e94e99d

Browse files
authored
Merge pull request #1703 from letalvoj/additional-for-comprehention-info
add additional information about for-comprehention desugaring
2 parents c366e40 + 7d71cb3 commit e94e99d

File tree

1 file changed

+24
-1
lines changed

1 file changed

+24
-1
lines changed

_overviews/FAQ/yield.md

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,31 @@ is translated into
6363

6464
c.map(x => (x, ...)).map((x,y) => {...})
6565

66+
#### Example 5
6667

67-
When you look at very simple for comprehensions, the map/foreach alternatives
68+
When pattern matching is used in for comprehensions on objects which do not
69+
implement `filter` or `withFilter` compilation fails with the following error
70+
71+
value withFilter is not a member of ...
72+
73+
That it because, for example, the following statement
74+
75+
for((a, b) <- c) yield {...}
76+
77+
is translated into
78+
79+
c.withFilter{
80+
case (a, b) => true
81+
case _ => false
82+
}.map{case (a, b) => {...}
83+
84+
where the `withFilter` ensures that the pattern in the subsequent function is
85+
always satisfied
86+
87+
Clarity
88+
----------------------------------
89+
90+
When you look at very simple for comprehensions, the `map`/`foreach` alternatives
6891
look, indeed, better. Once you start composing them, though, you can easily get
6992
lost in parenthesis and nesting levels. When that happens, for comprehensions
7093
are usually much clearer.

0 commit comments

Comments
 (0)