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

scope of temporary variable #35

Closed
weepy opened this issue Jan 5, 2010 · 2 comments
Closed

scope of temporary variable #35

weepy opened this issue Jan 5, 2010 · 2 comments
Labels

Comments

@weepy
Copy link

weepy commented Jan 5, 2010

Currently

lunch: eat(food) for food in ['toast', 'cheese', 'wine']

is converted to:

  var __a, __b, __c, __d, food, lunch;
  lunch = (function() {
    __a = ['toast', 'cheese', 'wine'];
    __c = [];
    for (__b in __a) {
      if (__a.hasOwnProperty(__b)) {
        food = __a[__b];
        __d = eat(food);
        __c.push(__d);
      }
    }
    return __c;
  })();

shouldn't it be:

  var food, lunch;
  lunch = (function() {
    var __a, __b, __c, __d;
    __a = ['toast', 'cheese', 'wine'];
    __c = [];
    for (__b in __a) {
      if (__a.hasOwnProperty(__b)) {
        food = __a[__b];
        __d = eat(food);
        __c.push(__d);
      }
    }
    return __c;
  })();
@jashkenas
Copy link
Owner

Hmm, that's a fair question -- we're actually taking special care in this case to ensure that variables within the comprehension are declared first outside of it. If you write a comprehension, but don't use it as part of an expression, then no closure wrapper is generated ... a comprehension isn't a function, in CoffeeScript, and doesn't imply function scope. Whether it needs to be wrapped in one or not is determined by where it occurs. It would be awfully strange if sometimes you could set variables from within a comprehension, and sometimes you couldn't -- so, to alleviate that, the auto-closures share a scope with their parent node. Sound alright?

@jashkenas
Copy link
Owner

No objections then ... closing the issue.

protez pushed a commit to protez/coffee-script that referenced this issue Jul 28, 2015
…s#35 by @astuchkin.  The change is that __iced_deferrals is now available to "custom" defer-handlers, like Rendezvous and others.  Also solves the ugly rv.__iced_deferrals.defer() hack...
This issue was closed.
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

2 participants