-
Notifications
You must be signed in to change notification settings - Fork 2k
Generating functions from comprehensions doesn't close over variables.. #423
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
Comments
I'm afraid that this is a feature... Comprehensions are supposed to have the same speed and roughly the same semantics as the equivalent JavaScript If you'd like them to, I'd recommend using
|
Ahhh bummer that sucks! If it ever comes up again I'm totally +1 in favour in breaking with semantics for this case! For perf.. could test for anon function at compile time and only wrap in this case? Though messy semantics grrr. But thanks for the response! I think using map makes the code even clearer anyway. |
That's nice, or you can keep it simple:
But yep, performance is the main thing here. It actually would be possible to test for function literals in the body of the loop... Do you think that would be desirable in all circumstances? |
"returns (variable at time of invocation)" to create an implicit closure isn't that bad of a feature. Is there any comparable keyword in any language? Edit: Or something like:
|
I think closing over loop variables is a great idea, and testing for function literals in the loop body would be a solid way to decide whether or not to capture them. |
Alright folks -- I think we have this one licked. Take a close look at this test case, because I want to make sure we have the right semantics here:
Here's the patch (includes some formatting): Closing the ticket. |
Cool! Awesome stuff. I couldn't think of or find any situations where you wouldn't want this behavior in the end. I'm still in support of the patch. Did come across some interesting links though:
Wowo! And in Firefox 3.6.3:
So I guess this patch is more of a feature than a bug fix. (cool feature though.)
|
@jashkenas: that test case definitely looks right. @foot : thanks for the illuminating language-design links! As a spectator on this issue tracker, it's great being able to place this stuff in context. |
I've raised an issue to discuss the removal of this feature, which has proven problematic: Issue 959. |
TEST:
[f1, f2]: (() -> n) for n in [1,2]
ok f1() is 1 # Fails (f1() is 2)
ok f2() is 2 # Passes
Coffee generates:
but should generate something like this probably?
Ta
The text was updated successfully, but these errors were encountered: