OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 // |
| 5 // Check that a variable declared and captured inside a loop is given a separate |
| 6 // context for each iteration of the loop, so changes to the variable in |
| 7 // subsequent iterations are not visible to closures capturing it in prior |
| 8 // iterations. |
| 9 |
| 10 void doit(int x) { |
| 11 final int max = 10; |
| 12 final double expectedSum = ((max - 1) * max) / 2; |
| 13 |
| 14 int counter = 0; |
| 15 var calls = []; |
| 16 while (counter < max) { |
| 17 int pos = counter; |
| 18 calls.add(() => pos + x); |
| 19 counter++; |
| 20 } |
| 21 |
| 22 double sum = 0.0; |
| 23 for (var c in calls) sum += c(); |
| 24 if (sum != expectedSum) |
| 25 throw new Exception("Unexpected sum = $sum != $expectedSum"); |
| 26 } |
| 27 |
| 28 void main() { |
| 29 doit(0); |
| 30 } |
OLD | NEW |