Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(67)

Unified Diff: pkg/kernel/testcases/interpreter/object_field_initializers_test.dart

Issue 2985883002: Add support for field initialization in objects (Closed)
Patch Set: Fix too long line Created 3 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: pkg/kernel/testcases/interpreter/object_field_initializers_test.dart
diff --git a/pkg/kernel/testcases/interpreter/object_field_initializers_test.dart b/pkg/kernel/testcases/interpreter/object_field_initializers_test.dart
new file mode 100644
index 0000000000000000000000000000000000000000..7910a338d4d66a9602e365425c0c0f67cc364e87
--- /dev/null
+++ b/pkg/kernel/testcases/interpreter/object_field_initializers_test.dart
@@ -0,0 +1,57 @@
+// Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file
+// for details. All rights reserved. Use of this source code is governed by a
+// BSD-style license that can be found in the LICENSE file.
+
+library object_field_initializers_test;
+
+/// Simple program creating an object with instance field initializers in the
+/// declaration of the class.
+///
+/// The static function `fieldInitializer` is used to ensure the fields are
+/// intialized in the correct order by tracking the order of side effects.
+void main() {
+ var a1 = new A.withoutArguments();
+ print(a1.foo1);
+ print(a1.foo2);
+ print(a1.foo3);
+ var a2 = new A('bar1', 'bar2');
+ print(a2.foo1);
+ print(a2.foo2);
+ print(a2.foo3);
+
+ var b = new B(fieldInitializer(1, 'bar1'), fieldInitializer(2, 'bar2'));
+ print(b.foo1);
+ print(b.foo2);
+ print(b.foo3);
+ print(b.foo4);
+}
+
+class A {
+ String foo1 = 'foo1';
+ String foo2;
+ String foo3 = 'foo3';
+
+ A.withoutArguments();
+ A(String foo1, String foo2) {
+ this.foo1 = foo1;
+ this.foo2 = foo2;
+ }
+}
+
+class B {
+ String foo1 = fieldInitializer(3, 'foo1');
+ String foo2;
+ String foo3 = fieldInitializer(4, 'foo3');
+ String foo4 = fieldInitializer(5, 'foo4');
+
+ B(String foo1, String foo2) {
+ this.foo1 = foo1;
+ this.foo2 = foo2;
+ this.foo4 = fieldInitializer(6, 'bar4');
+ }
+}
+
+String fieldInitializer(int f, String s) {
+ print('$s: $f');
+ return '$s: $f';
+}

Powered by Google App Engine
This is Rietveld 408576698