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, ... |
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) |
| 36 : b1 = E(x++), |
| 37 super(1000), |
| 38 b2 = E(y++) { |
36 // Implicit super call to A's body happens here. | 39 // Implicit super call to A's body happens here. |
37 E(x); | 40 E(x); |
38 E(y); | 41 E(y); |
39 f(); | 42 f(); |
40 } | 43 } |
41 var b1; | 44 var b1; |
42 var b2; | 45 var b2; |
43 } | 46 } |
44 | 47 |
45 class C extends B { | 48 class C extends B { |
46 C() : super(10, 20); | 49 C() : super(10, 20); |
47 } | 50 } |
48 | 51 |
49 main() { | 52 main() { |
50 var c = new C(); | 53 var c = new C(); |
51 Expect.equals("10-1001-20-1011-1111-1121-11-21-1131-", trace); | 54 Expect.equals("10-1001-20-1011-1111-1121-11-21-1131-", trace); |
52 } | 55 } |
OLD | NEW |