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 loop variable capture is inconsistent between 'for in' and 'for' #333

Closed
DartBot opened this issue Nov 4, 2011 · 9 comments
Closed
Labels
closed-not-planned Closed as we don't intend to take action on the reported issue P3 A lower priority bug or feature request type-enhancement A request for a change that isn't a bug web-dart2js

Comments

@DartBot
Copy link

DartBot commented Nov 4, 2011

This issue was originally filed by [email protected]


I notice that the loop variable in a 'for' loop behaves differently than the loop variable in a 'for in' loop, when the loop variable is captured by a closure. I think the 'for in' behavior is what we want, and we should change the 'for' loop behavior to do what 'for in' does.

For example, here is output from the program below that demonstrates this. Here 'a' is the loop variable, and 'b' is a local block variable. Notice 'b' gets captured in both cases, but 'a' is only captured with 'for in' loops.

start
using normal for loop:
a is 5, b is 0
a is 5, b is 1
a is 5, b is 2
a is 5, b is 3
a is 5, b is 4
using for in loop:
a is 0, b is 0
a is 1, b is 1
a is 2, b is 2
a is 3, b is 3
a is 4, b is 4
done

From this program:

class Capture {

  static void main() {
    print("start");
    doForLoop();
    doForIn();
    print ("done");
  }

  static void doForLoop() {
    print("using normal for loop:");
    List closures = new List();
    for (int a = 0; a < 5; a++) {
      int b = a;
      closures.add( (){print("a is ${a}, b is ${b}");} );
    }
    for (var c in closures) {
      c();
    }
  }

  static void doForIn() {

    print("using for in loop:");
    List numbers = new List();
    for (int i = 0; i < 5; i++) {
      numbers.add(i);
    }
    List closures = new List();
    for (int a in numbers) {
      int b = a;
      closures.add( (){print("a is ${a}, b is ${b}");} );
    }
    for (var c in closures) {
      c();
    }
  }
}

void main() {
  Capture.main();
}

@kasperl
Copy link

kasperl commented Nov 4, 2011

This is already covered by section 11.5.1 ("For Loop") in the specification. It just needs to be implemented in the VM and the compilers.

@DartBot
Copy link
Author

DartBot commented Nov 4, 2011

This comment was originally written by [email protected]


Removed Type-Defect label.
Added Type-Enhancement, Area-VM, Triaged labels.

@DartBot
Copy link
Author

DartBot commented Nov 16, 2011

This comment was originally written by [email protected]


I'm changing the area to frog, just to make sure frog side sees this, and there are some closely related bugs in frog that might make fixing this one easy at the same time. (Let me know if we want separate bugs for vm and frog on this, but I think it's probably easiest to track in one place.)


Set owner to [email protected].
Removed Area-VM label.
Added Area-Frog label.

@DartBot
Copy link
Author

DartBot commented Nov 22, 2011

This comment was originally written by [email protected]


Marking as low pri as this would be okay to let slip until next quarter. Please edit the priority if you think this is critical to using frog in the near term.


Removed Priority-Medium label.
Added Priority-Low label.

@DartBot
Copy link
Author

DartBot commented Nov 28, 2011

This comment was originally written by [email protected]


Removed the owner.

@DartBot
Copy link
Author

DartBot commented Jan 11, 2012

This comment was originally written by @sethladd


This appears to be working in the VM now, but not in DartC (try.dartlang.org) or Frog.

Code tested:

void main() {
  var callbacks = [];
  for (var i = 0; i < 10; i++) {
    callbacks.add(() => print(i));
  }
  callbacks.forEach((c) => c());
}

@DartBot
Copy link
Author

DartBot commented Mar 26, 2012

This comment was originally written by [email protected]


Also observed this not working at try.dartlang.org... until it is fixed, would it make sense to add a warning to the language tour page under the example bragging about how Dart fixes this relative to JavaScript? :)

@anders-sandholm
Copy link
Contributor

Removed Area-Frog label.
Added Area-Dart2JS, FromAreaFrog labels.

@kasperl
Copy link

kasperl commented Jun 12, 2012

Added WontFix label.

This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
closed-not-planned Closed as we don't intend to take action on the reported issue P3 A lower priority bug or feature request type-enhancement A request for a change that isn't a bug web-dart2js
Projects
None yet
Development

No branches or pull requests

4 participants