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 // Test that generic methods can be overloaded (a) with widened type bounds, and | 5 // Test that generic methods can be overloaded (a) with widened type bounds, and |
6 // (b) using the bound as the type of the parameter in the overloaded method. | 6 // (b) using the bound as the type of the parameter in the overloaded method. |
7 | 7 |
8 library generic_methods_overriding_test; | 8 library generic_methods_overriding_test; |
9 | 9 |
10 import "package:expect/expect.dart"; | 10 import "package:expect/expect.dart"; |
11 | 11 |
12 class X {} | 12 class X {} |
13 | 13 |
14 class Y extends X {} | 14 class Y extends X {} |
15 | 15 |
16 class Z extends Y {} | 16 class Z extends Y {} |
17 | 17 |
18 class C { | 18 class C { |
19 String fun<T extends Y>(T t) => "C"; | 19 String fun<T extends Y>(T t) => "C"; |
20 } | 20 } |
21 | 21 |
22 class D extends C { | 22 class D extends C { |
23 String fun<T extends X>(T t) => "D"; /// 01: compile-time error | 23 String fun<T extends X>(T t) => "D"; //# 01: compile-time error |
24 String fun<T extends Y>(T t) => "D"; /// 02: ok | 24 String fun<T extends Y>(T t) => "D"; //# 02: ok |
25 } | 25 } |
26 | 26 |
27 class E extends C { | 27 class E extends C { |
28 String fun<T>(Y y) => "E"; /// 03: compile-time error | 28 String fun<T>(Y y) => "E"; //# 03: compile-time error |
29 String fun<T extends Y>(Y y) => "E"; /// 04: ok | 29 String fun<T extends Y>(Y y) => "E"; //# 04: ok |
30 } | 30 } |
31 | 31 |
32 class F extends C { | 32 class F extends C { |
33 String foobar(Z z) { | 33 String foobar(Z z) { |
34 return "FZ"; | 34 return "FZ"; |
35 } | 35 } |
36 | 36 |
37 String fun<T extends Y>(T t) { | 37 String fun<T extends Y>(T t) { |
38 if (t is Z) { | 38 if (t is Z) { |
39 return this.foobar(t as Z); /// 05: ok | 39 return this.foobar(t as Z); //# 05: ok |
40 return this.foobar(t); /// 06: ok | 40 return this.foobar(t); //# 06: ok |
41 } | 41 } |
42 return "FY"; | 42 return "FY"; |
43 } | 43 } |
44 } | 44 } |
45 | 45 |
46 main() { | 46 main() { |
47 Y y = new Y(); | 47 Y y = new Y(); |
48 Z z = new Z(); | 48 Z z = new Z(); |
49 | 49 |
50 C c = new C(); | 50 C c = new C(); |
51 D d = new D(); | 51 D d = new D(); |
52 E e = new E(); | 52 E e = new E(); |
53 F f = new F(); | 53 F f = new F(); |
54 | 54 |
55 Expect.equals(c.fun<Y>(y), "C"); | 55 Expect.equals(c.fun<Y>(y), "C"); |
56 Expect.equals(d.fun<Y>(y), "D"); /// 02: continued | 56 Expect.equals(d.fun<Y>(y), "D"); //# 02: continued |
57 Expect.equals(e.fun<Y>(y), "E"); /// 04: continued | 57 Expect.equals(e.fun<Y>(y), "E"); //# 04: continued |
58 Expect.equals(f.fun<Y>(y), "FY"); /// 05: continued | 58 Expect.equals(f.fun<Y>(y), "FY"); //# 05: continued |
59 Expect.equals(f.fun<Z>(z), "FZ"); /// 05: continued | 59 Expect.equals(f.fun<Z>(z), "FZ"); //# 05: continued |
60 Expect.equals(f.fun<Y>(y), "FY"); /// 06: continued | 60 Expect.equals(f.fun<Y>(y), "FY"); //# 06: continued |
61 Expect.equals(f.fun<Z>(z), "FZ"); /// 06: continued | 61 Expect.equals(f.fun<Z>(z), "FZ"); //# 06: continued |
62 } | 62 } |
OLD | NEW |