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

Loop behavior altered by continue and return #954

Closed
TrevorBurnham opened this issue Dec 19, 2010 · 7 comments
Closed

Loop behavior altered by continue and return #954

TrevorBurnham opened this issue Dec 19, 2010 · 7 comments

Comments

@TrevorBurnham
Copy link
Collaborator

Following up issue 948:

On the current master,

for i in [1, 2, 3]
  j = i
  func1 = -> console.log "i is #{i} and j is #{j}"
  setTimeout func1, 0

gives you

i is 1 and j is 1
i is 2 and j is 2
i is 3 and j is 3

but if you add a continue or return statement anywhere in the loop, the behavior is radically changed, e.g.:

for i in [1, 2, 3]
  j = i
  func1 = -> console.log "i is #{i} and j is #{j}"
  setTimeout func1, 0
  continue

gives you

i is 3 and j is 3
i is 3 and j is 3
i is 3 and j is 3

as would the same code with, say, return if false in place of continue.

Let's look at the compiled output without the continue/return:

var i, _fn, _i, _len, _ref;
_ref = [1, 2, 3];
_fn = function(i) {
  var func1, j;
  j = i;
  func1 = function() {
    return console.log("i is " + i + " and j is " + j);
  };
  setTimeout(func1, 0);
};
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
  i = _ref[_i];
  _fn(i);
}

Currently, the whole _fn thing isn't done if there's a continue or return anywhere in the loop.

One solution would be to replace _fn(i); with retVal = _fn(i); retVal ? return retVal[0] : 0;, and then compile continue to return, return to return [void 0];, and return value to return [value]. (Edit: This proposal wasn't tested and needs more work; see satyr's comment below.)

@satyr
Copy link
Collaborator

satyr commented Dec 19, 2010

And break to...?

retVal ? return retVal[0] : 0

Syntax error.

@TrevorBurnham
Copy link
Collaborator Author

Good points both. Do you have an alternative solution in mind?

@satyr
Copy link
Collaborator

satyr commented Dec 19, 2010

an alternative solution

Remove this implicit transformation. (I'm glad Coco did.)

@jashkenas
Copy link
Owner

I'm afraid that this inconsistency is a necessary part of the implicit transformation. As long as we have functions-in-loops that close over their variables, we'll have to have this as well.

The current situation with pure statements like continue, break, and return is that they cancel the closure wrapping, because they have no meaning inside of it.

@jashkenas
Copy link
Owner

This is a re-hashing of issues #423, and #728. Please take a peek at those.

@odf
Copy link

odf commented Dec 21, 2010

I have an alternative alternative solution: remove continue, break and return from the language. I think fixing the scope issues in loops is more important than keeping obsolete control flow hacks alive indefinitely. ;-)

Failing that, just document the issue and leave things as they are.

@jashkenas
Copy link
Owner

Thanks for wrapping all of this up in #959, Trevor. Closing this ticket in favor of that one.

This issue was closed.
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

4 participants