Skip to content

add additional information about for-comprehension desugaring #1703

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 17, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 24 additions & 1 deletion _overviews/FAQ/yield.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,31 @@ is translated into

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

#### Example 5

When you look at very simple for comprehensions, the map/foreach alternatives
When pattern matching is used in for comprehensions on objects which do not
implement `filter` or `withFilter` compilation fails with the following error

value withFilter is not a member of ...

That it because, for example, the following statement

for((a, b) <- c) yield {...}

is translated into

c.withFilter{
case (a, b) => true
case _ => false
}.map{case (a, b) => {...}

where the `withFilter` ensures that the pattern in the subsequent function is
always satisfied

Clarity
----------------------------------

When you look at very simple for comprehensions, the `map`/`foreach` alternatives
look, indeed, better. Once you start composing them, though, you can easily get
lost in parenthesis and nesting levels. When that happens, for comprehensions
are usually much clearer.
Expand Down