OLD | NEW |
---|---|
1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
4 // Dart test program for constructors and initializers. | 4 // Dart test program for constructors and initializers. |
5 | 5 |
6 import "package:expect/expect.dart"; | 6 import "package:expect/expect.dart"; |
7 | 7 |
8 // Super initializer and super constructor body are executed in with the same | 8 // Super initializer and super constructor body are executed in with the same |
9 // bindings. | 9 // bindings. |
10 | 10 |
11 String trace = ""; | 11 String trace = ""; |
12 | 12 |
13 int E(int i) { | 13 int E(int i) { |
14 trace = "$trace$i-"; | 14 trace = "$trace$i-"; |
15 return i; | 15 return i; |
16 } | 16 } |
17 | 17 |
18 class A { | 18 class A { |
19 // f closes-over arg. arg needs to be preserved while b2 is initialized. | 19 // f closes-over arg. arg needs to be preserved while b2 is initialized. |
20 A(arg) | 20 A(arg) |
21 : a = E(arg += 1) | 21 : a = E(arg += 1) |
22 , f = (() => E(arg += 10)) { | 22 , f = (() => E(arg += 10)) { |
23 // b2 should be initialized between the above initializers and the following | 23 // b2 should be initialized between the above initializers and the following |
24 // statements. | 24 // statements. |
25 var r1 = f(); | 25 var r1 = f(); |
26 E(arg += 100); // If this is the same arg as closed by f, ... | 26 E(arg += 100); // If this is the same arg as closed by f, ... |
sra1
2017/03/21 03:28:14
fix
| |
27 var r2 = f(); // .. the effect of +=100 will be seen here. | 27 var r2 = f(); // .. the effect of +=100 will be seen here. |
28 } | 28 } |
29 final a; | 29 final a; |
30 final f; | 30 final f; |
31 } | 31 } |
32 | 32 |
33 class B extends A { | 33 class B extends A { |
34 // Initializers in order: b1, super, b2. | 34 // Initializers in order: b1, super, b2. |
35 B(x, y) : b1 = E(x++), super(1000), b2 = E(y++) { | 35 B(x, y) : b1 = E(x++), super(1000), b2 = E(y++) { |
36 // Implicit super call to A's body happens here. | 36 // Implicit super call to A's body happens here. |
37 E(x); | 37 E(x); |
38 E(y); | 38 E(y); |
39 f(); | 39 f(); |
40 } | 40 } |
41 var b1; | 41 var b1; |
42 var b2; | 42 var b2; |
43 } | 43 } |
44 | 44 |
45 class C extends B { | 45 class C extends B { |
46 C() : super(10, 20); | 46 C() : super(10, 20); |
47 } | 47 } |
48 | 48 |
49 main() { | 49 main() { |
50 var c = new C(); | 50 var c = new C(); |
51 Expect.equals("10-1001-20-1011-1111-1121-11-21-1131-", trace); | 51 Expect.equals("10-1001-20-1011-1111-1121-11-21-1131-", trace); |
52 } | 52 } |
OLD | NEW |