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 comprehension doesn't works with return and yield #3357

Closed
al6x opened this issue Feb 6, 2014 · 12 comments
Closed

for comprehension doesn't works with return and yield #3357

al6x opened this issue Feb 6, 2014 · 12 comments
Labels

Comments

@al6x
Copy link

al6x commented Feb 6, 2014

This code doesn't compiles

f = ->
  return for v in list
    v
@mklement0
Copy link
Contributor

It will compile (at least on 1.7.1) if you align the last line (v) with the one above, but the return for v in list doesn't make sense, as it will return (and thus exit the function) after the first iteration (you're effectively saying: "return for each element of list", which, of course, only returns the first element).

You probably want:

f = ->
  v for v in list

Or:

This relies on CoffeeScript's implicit returns combined with the [almost]-everything-is-an-expression philosophy - the explicit, non-idiomatic equivalent would be return (v for v in list).

(Of course, unless you plan on actually operating on the elements in list, you may as well just do f = -> list.)

Edit: You probably meant the prefix form of the comprehension; the approach is the same, though:

f = ->
  for v in list
    v

@al6x
Copy link
Author

al6x commented Feb 7, 2014

You probably want: v for v in list

Exactly, I want to return another list after doing some modifications to v in loop. But since it's possible to write it this way return v^2 for v in list would be nice to have it also for

return for v in list
  v^2 

It's not a "new line" after return. There's an indentation, it's part of the loop, not a "new line".

You probably meant the postpositional form of the comprehension; the approach is the same, though:

Yes, and as soon as it works with implicit return - it looks strange why it stop working when you put that return explicitly.

@michaelficarra michaelficarra reopened this Feb 7, 2014
@mklement0
Copy link
Contributor

Pragmatically speaking:

  • In the spirit of CoffeeScript: I suggest not using return - see my last code snippet above.
  • If you still want to, enclose the entire for expression in parentheses.

In case you're raising a language-design question: I'll let others who know more about the language answer, but I can say this: at the very least there is ambiguity: return for v in list is by itself a complete statement, and that's what the compiler currently assumes.

(As an aside: maybe you're fully aware of this, but note that ^ is the XOR bitwise operator in CoffeeScript/JavaScript; perhaps you meant v*v or, more generally, Math.pow v, 2.)

@michaelficarra
Copy link
Collaborator

@mklement0: When followed by a block, the meaning changes. Also consider return for v in list then v ** 2. Also note the ** exponentiation operator, added in 1.7.0.

@michaelficarra michaelficarra added bug and removed invalid labels Feb 7, 2014
@mklement0
Copy link
Contributor

@michaelficarra: Thanks for the tips - very handy.

return for v in list then v ** 2 actually breaks (in 1.7.1) without parentheses.

When followed by a block, the meaning changes.

Can you please elaborate? Are you saying that what @alexeyPetrushin is trying to do should work?

@michaelficarra
Copy link
Collaborator

Yes, which is why I reopened this issue. No parentheses should be needed when a block follows the comprehension header. These examples all work in CoffeeScriptRedux.

@mklement0
Copy link
Contributor

@michaelficarra: Got it, thanks. So return for v in list then v ** 2 breaking without parentheses is a manifestation of the same bug?

@michaelficarra
Copy link
Collaborator

Yep.

@xixixao
Copy link
Contributor

xixixao commented Feb 9, 2014

Related: throw for v in list then v compiles to invalid JS.

@al6x
Copy link
Author

al6x commented Feb 9, 2014

@xixixao seems like inconsistent behaviour of throw is by design #3347

@xixixao
Copy link
Contributor

xixixao commented Feb 9, 2014

That's a different issue. We shouldn't be generating invalid JS.

@lydell lydell changed the title for comprehension doesn't works with return for comprehension doesn't works with return and yield Sep 14, 2015
@GeoffreyBooth
Copy link
Collaborator

The initial example returns an “unexpected indentation” error in CS2, so I think that resolves this particular issue.

Added @xixixao’s invalid JS example to #3709, which appears to be related if not identical.

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

No branches or pull requests

6 participants