OLD | NEW |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, 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 import "package:expect/expect.dart"; | 5 import "package:expect/expect.dart"; |
6 | 6 |
7 abstract class A { | 7 abstract class A { |
8 factory A(int x, int y) = B; | 8 factory A(int x, int y) = B; |
9 } | 9 } |
10 | 10 |
11 abstract class X { | 11 abstract class X { |
12 factory X(int x, int y) = B.X; | 12 factory X(int x, int y) = B.X; |
13 } | 13 } |
14 | 14 |
15 class XImpl implements X { | 15 class XImpl implements X { |
16 final int x; | 16 final int x; |
17 final int y; | 17 final int y; |
18 XImpl(this.x, this.y); | 18 XImpl(this.x, this.y); |
19 } | 19 } |
20 | 20 |
21 class B implements A { | 21 class B implements A { |
22 final int x; | 22 final int x; |
23 final int y; | 23 final int y; |
24 | 24 |
25 B(this.x, this.y); | 25 B(this.x, this.y); |
26 // This factory will never be invoked. | 26 // This factory will never be invoked. |
27 // TODO(ahe): Is this a compile time error? | 27 // TODO(ahe): Is this a compile time error? |
28 factory B.A(int a, int b) { | 28 factory B.A(int a, int b) { return new B(0, 0); } |
29 return new B(0, 0); | |
30 } | |
31 | 29 |
32 factory B.X(int a, int b) { | 30 factory B.X(int a, int b) { return new XImpl(a * 10, b * 10); } |
33 return new XImpl(a * 10, b * 10); | |
34 } | |
35 } | 31 } |
36 | 32 |
37 main() { | 33 main() { |
38 var a = new A(1, 2); | 34 var a = new A(1, 2); |
39 // Check that constructor B is invoked and not factory B.A. | 35 // Check that constructor B is invoked and not factory B.A. |
40 Expect.equals(1, a.x); | 36 Expect.equals(1, a.x); |
41 Expect.equals(2, a.y); | 37 Expect.equals(2, a.y); |
42 | 38 |
43 var x = new X(11, 22); // //# 00: dynamic type error | 39 var x = new X(11, 22); // //# 00: dynamic type error |
44 // Check that factory is invoked. | 40 // Check that factory is invoked. |
45 Expect.equals(110, x.x); // //# 00: continued | 41 Expect.equals(110, x.x); // //# 00: continued |
46 Expect.equals(220, x.y); // //# 00: continued | 42 Expect.equals(220, x.y); // //# 00: continued |
47 } | 43 } |
OLD | NEW |