-
Notifications
You must be signed in to change notification settings - Fork 21
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
please allow value definitions before the first generator in a for comprehension #907
Comments
Imported From: https://issues.scala-lang.org/browse/SI-907?orig=1 |
@paulp said: |
Samuel Halliday (fommil) said: e.g. rewrite Seth's original example as val x = foo(bar)
for {y <- x} |
I'd really like to see this ticket reopened. |
I'm not sure this workaround: val x = foo(bar)
for {y <- x} is worth it. I think we have already enough desugarings in parser. Putting more would force all the Scala tooling to catch up and I don't think it's a neat solution overall, but rather a hack. |
for FP code this is definitely worth it. This comes up all the time when |
btw, apparently we should be discussing at https://contributors.scala-lang.org/t/ressurrect-assignment-as-first-line-of-for/808 |
it appears this was closed because the core team at the time didn't intend to tackle it, not because it would necessarily be rejected if it came in as a community contribution (including the necessary spec work and so forth) |
an example usage with |
further food for thought in this area at https://contributors.scala-lang.org/t/pre-sip-let-expressions-in-scala/1391/1 |
I would like to go even further and allow for comprehensions with only value definitions and no generators. It would be like defining a bunch of One of the really nice things about Scala is that changing code in a limited way often involves few edits. For instance to change a binding from immediately-evaluate-once to deferred-evaluate-every-time you just change Additionally, most constructs don't have a very strict, conservative set of ways to apply it. Constructs are usually limited only for very good reasons. For instance you can nest methods inside classes inside pattern matches etc. Vals can have block statements on their right-hand side. And there isn't different syntax for class members vs. local bindings. Together this means that you can take a more "agile" approach to writing code, not worrying up front what form things will take because you can easily evolve code over time with little effort. By the same token, to me it makes sense that since for comprehension already supports value definitions, there's no good reason to prohibit them as the first item. And once you accept that, there's no good reason to require it to be followed by a generator. Because suppose you're editing a for comprehension and you end up deleting the first generator, which has value definitions after it. As it is, now you may have to suddenly create curly braces around it and move the definitions there and add the val keyword. There's no reason to force that busy work when the meaning is clear. And if you do that, then by the same token if later you delete the subsequent generator, leaving only value definitions, there's no reason the compiler has to force the busy work of converting all the value definitions to explicit vals. As an added bonus, there are contexts where using |
came up again at https://contributors.scala-lang.org/t/pre-sip-comprehensive-function-applications/5902/12 where initial args are precomputed. |
closing since this would need to happen in Scala 3 first before we could do it here |
Closing because the ticket has 22 thumbs-up, which is the limit for Scala 2. Since it is an enhancement, not a bug, feature proposals with implementations and backports are always welcome. |
scala/improvement-proposals#79 proposes to fix this in Scala 3 |
I would like to be able to write this:
for{x = foo(bar); y <- x} ...
the language spec (6.19) says "An enumerator sequence always starts with
a generator", but why? From time to time I would find it convenient to put
a value definition first. I realize that in the above code I only use x once,
but I've hit this in real code where I want to use x more than once.
(Perhaps "if" should be allowed too?)
The text was updated successfully, but these errors were encountered: