| OLD | NEW |
| 1 // Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file | 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 | 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 | 4 |
| 5 library object_field_initializers_test; | 5 library object_field_initializers_test; |
| 6 | 6 |
| 7 /// Simple program creating an object with instance field initializers in the | 7 /// Simple program creating an object with instance field initializers in the |
| 8 /// declaration of the class. | 8 /// declaration of the class. |
| 9 /// | 9 /// |
| 10 /// The static function `fieldInitializer` is used to ensure the fields are | 10 /// The static function `fieldInitializer` is used to ensure the fields are |
| 11 /// intialized in the correct order by tracking the order of side effects. | 11 /// intialized in the correct order by tracking the order of side effects. |
| 12 void main() { | 12 void main() { |
| 13 print('Create instance of A'); |
| 13 var a1 = new A.withoutArguments(); | 14 var a1 = new A.withoutArguments(); |
| 14 print(a1.foo1); | 15 print(a1.foo1); |
| 15 print(a1.foo2); | 16 print(a1.foo2); |
| 16 print(a1.foo3); | 17 print(a1.foo3); |
| 17 var a2 = new A('bar1', 'bar2'); | 18 var a2 = new A('bar1', 'bar2'); |
| 18 print(a2.foo1); | 19 print(a2.foo1); |
| 19 print(a2.foo2); | 20 print(a2.foo2); |
| 20 print(a2.foo3); | 21 print(a2.foo3); |
| 21 | 22 |
| 23 print('Create instance of B'); |
| 22 var b = new B(fieldInitializer(1, 'bar1'), fieldInitializer(2, 'bar2')); | 24 var b = new B(fieldInitializer(1, 'bar1'), fieldInitializer(2, 'bar2')); |
| 23 print(b.foo1); | 25 print(b.foo1); |
| 24 print(b.foo2); | 26 print(b.foo2); |
| 25 print(b.foo3); | 27 print(b.foo3); |
| 26 print(b.foo4); | 28 print(b.foo4); |
| 29 |
| 30 print('Create instance of C'); |
| 31 var c = new C(fieldInitializer(1, 'bar1'), fieldInitializer(2, 'bar2')); |
| 32 print(c.foo1); |
| 33 print(c.foo2); |
| 34 print(c.foo3); |
| 27 } | 35 } |
| 28 | 36 |
| 29 class A { | 37 class A { |
| 30 String foo1 = 'foo1'; | 38 String foo1 = 'foo1'; |
| 31 String foo2; | 39 String foo2; |
| 32 String foo3 = 'foo3'; | 40 String foo3 = 'foo3'; |
| 33 | 41 |
| 34 A.withoutArguments(); | 42 A.withoutArguments(); |
| 35 A(String foo1, String foo2) { | 43 A(String foo1, String foo2) { |
| 36 this.foo1 = foo1; | 44 this.foo1 = foo1; |
| 37 this.foo2 = foo2; | 45 this.foo2 = foo2; |
| 38 } | 46 } |
| 39 } | 47 } |
| 40 | 48 |
| 41 class B { | 49 class B { |
| 42 String foo1 = fieldInitializer(3, 'foo1'); | 50 String foo1 = fieldInitializer(3, 'foo1'); |
| 43 String foo2; | 51 String foo2; |
| 44 String foo3 = fieldInitializer(4, 'foo3'); | 52 String foo3 = fieldInitializer(4, 'foo3'); |
| 45 String foo4 = fieldInitializer(5, 'foo4'); | 53 String foo4 = fieldInitializer(5, 'foo4'); |
| 46 | 54 |
| 47 B(String foo1, String foo2) { | 55 B(String foo1, String foo2) { |
| 48 this.foo1 = foo1; | 56 this.foo1 = foo1; |
| 49 this.foo2 = foo2; | 57 this.foo2 = foo2; |
| 50 this.foo4 = fieldInitializer(6, 'bar4'); | 58 this.foo4 = fieldInitializer(6, 'bar4'); |
| 51 } | 59 } |
| 52 } | 60 } |
| 53 | 61 |
| 62 class C { |
| 63 String foo1 = fieldInitializer(1, 'foo1'); |
| 64 String foo2; |
| 65 String foo3 = fieldInitializer(2, 'foo3'); |
| 66 |
| 67 C(this.foo1, this.foo2) { |
| 68 foo3 = fieldInitializer(3, 'foo3'); |
| 69 } |
| 70 } |
| 71 |
| 54 String fieldInitializer(int f, String s) { | 72 String fieldInitializer(int f, String s) { |
| 55 print('$s: $f'); | 73 print('$s: $f'); |
| 56 return '$s: $f'; | 74 return '$s: $f'; |
| 57 } | 75 } |
| OLD | NEW |