Skip to content
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

For-comp with no yield synthesises a .map call #11951

Closed
japgolly opened this issue Apr 23, 2020 · 6 comments
Closed

For-comp with no yield synthesises a .map call #11951

japgolly opened this issue Apr 23, 2020 · 6 comments

Comments

@japgolly
Copy link

reproduction steps

Desugar the following:

for {x <- List(1); y = 1} ()

problem

It effectively desugars to:

List(1).map { x => val y = 1; (x, y) }.foreach { case (x, y) => () }

expectation

Because there's no yield, I expect all .flatMap and .map calls to be .foreach instead. It should desugar to:

List(1).foreach { x => val y = 1; () }
@Jasper-M
Copy link

Isn't that more or less the general issue that assignments (y=1) in for comprehensions incur an extra map call?

@joroKr21
Copy link
Member

@martijnhoekstra
Copy link

martijnhoekstra commented Apr 23, 2020

working as specced at https://scala-lang.org/files/archive/spec/2.13/06-expressions.html#for-comprehensions-and-for-loops to everyones chagrin as

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:

(p, p′) <- for (x@p <- e) yield { val x′@p′ = e′; (x, x′) }

https://contributors.scala-lang.org/t/pre-sip-improve-for-comprehensions-functionality/3509/52 is the canonical contributors thread.

@dwijnand dwijnand added this to the Backlog milestone Nov 20, 2020
@dwijnand
Copy link
Member

Perhaps scala/scala3#2573 is the canonical epic.

@SethTisue
Copy link
Member

unlikely to progress in Scala 2. Scala 3 is a different story

@SethTisue SethTisue closed this as not planned Won't fix, can't repro, duplicate, stale Mar 30, 2023
@SethTisue SethTisue removed this from the Backlog milestone Mar 30, 2023
@SethTisue
Copy link
Member

(if it ever were to progress in Scala 2, it would be because it happened in Scala 3 first)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants