OLD | NEW |
1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2016, 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 // VMOptions=--generic-method-syntax | 5 // VMOptions=--generic-method-syntax |
6 | 6 |
7 /// Dart test on the usage of method type arguments in type expressions. With | 7 /// Dart test on the usage of method type arguments in type expressions. With |
8 /// '--generic-method-syntax', the type argument is available at runtime, | 8 /// '--generic-method-syntax', the type argument is available at runtime, |
9 /// but erased to `dynamic`. | 9 /// but erased to `dynamic`. |
10 | 10 |
11 library generic_methods_type_expression_test; | 11 library generic_methods_type_expression_test; |
12 | 12 |
13 import "package:expect/expect.dart"; | 13 import "package:expect/expect.dart"; |
14 | 14 |
15 bool f1<T>(Object o) => o is T; /// 01: static type warning | 15 bool f1<T>(Object o) => o is T; //# 01: static type warning |
16 | 16 |
17 bool f2<T>(Object o) => o is List<T>; | 17 bool f2<T>(Object o) => o is List<T>; |
18 | 18 |
19 bool f3<T>(Object o) => o is! T; /// 03: static type warning | 19 bool f3<T>(Object o) => o is! T; //# 03: static type warning |
20 | 20 |
21 bool f4<T>(Object o) => o is! List<T>; | 21 bool f4<T>(Object o) => o is! List<T>; |
22 | 22 |
23 T f5<T>(Object o) => o as T; | 23 T f5<T>(Object o) => o as T; |
24 | 24 |
25 List<T> f6<T>(Object o) => o as List<T>; | 25 List<T> f6<T>(Object o) => o as List<T>; |
26 | 26 |
27 Type f7<T>() => T; | 27 Type f7<T>() => T; |
28 | 28 |
29 class TypeValue<X> { | 29 class TypeValue<X> { |
30 Type get value => X; | 30 Type get value => X; |
31 } | 31 } |
32 | 32 |
33 Type f8<T>() => new TypeValue<List<T>>().value; | 33 Type f8<T>() => new TypeValue<List<T>>().value; |
34 | 34 |
35 bool f9<T>(Object o) => o is Map<T, String>; | 35 bool f9<T>(Object o) => o is Map<T, String>; |
36 | 36 |
37 class IsMap<A> { | 37 class IsMap<A> { |
38 @NoInline() | 38 @NoInline() |
39 bool check<B>(o) => o is Map<A, B>; | 39 bool check<B>(o) => o is Map<A, B>; |
40 } | 40 } |
41 | 41 |
42 main() { | 42 main() { |
43 String s = "Hello!"; | 43 String s = "Hello!"; |
44 List<String> ss = <String>[s]; | 44 List<String> ss = <String>[s]; |
45 Expect.throws(() => f1<int>(42), (e) => e is TypeError); /// 01: continued | 45 Expect.throws(() => f1<int>(42), (e) => e is TypeError); //# 01: continued |
46 Expect.throws(() => f1<String>(42), (e) => e is TypeError); /// 01: continued | 46 Expect.throws(() => f1<String>(42), (e) => e is TypeError); //# 01: continued |
47 Expect.equals(f2<int>(<int>[42]), true); | 47 Expect.equals(f2<int>(<int>[42]), true); |
48 Expect.equals(f2<String>(<int>[42]), true); // `is List<dynamic>` is true. | 48 Expect.equals(f2<String>(<int>[42]), true); // `is List<dynamic>` is true. |
49 Expect.throws(() => f3<int>(42), (e) => e is TypeError); /// 03: continued | 49 Expect.throws(() => f3<int>(42), (e) => e is TypeError); //# 03: continued |
50 Expect.throws(() => f3<String>(42), (e) => e is TypeError); /// 03: continued | 50 Expect.throws(() => f3<String>(42), (e) => e is TypeError); //# 03: continued |
51 Expect.equals(f4<int>(<int>[42]), false); | 51 Expect.equals(f4<int>(<int>[42]), false); |
52 Expect.equals(f4<String>(<int>[42]), false); // `is! List<dynamic>` is false. | 52 Expect.equals(f4<String>(<int>[42]), false); // `is! List<dynamic>` is false. |
53 Expect.equals(f5<String>(s), s); // `s as dynamic == s` | 53 Expect.equals(f5<String>(s), s); // `s as dynamic == s` |
54 Expect.equals(f5<int>(s), s); // `s as dynamic == s` | 54 Expect.equals(f5<int>(s), s); // `s as dynamic == s` |
55 Expect.equals(f6<String>(ss), ss); | 55 Expect.equals(f6<String>(ss), ss); |
56 Expect.equals(f6<int>(ss), ss); // `as List<dynamic>` succeeds. | 56 Expect.equals(f6<int>(ss), ss); // `as List<dynamic>` succeeds. |
57 Expect.throws(() => f7<int>(), (e) => e is TypeError); | 57 Expect.throws(() => f7<int>(), (e) => e is TypeError); |
58 Expect.equals(f8<int>(), List); // Returns `List<dynamic>`. | 58 Expect.equals(f8<int>(), List); // Returns `List<dynamic>`. |
59 | 59 |
60 Expect.isTrue(f9<int>(<int,String>{})); | 60 Expect.isTrue(f9<int>(<int,String>{})); |
61 Expect.isTrue(f9<int>(<bool,String>{})); // `is Map<dynamic, String>` is true. | 61 Expect.isTrue(f9<int>(<bool,String>{})); // `is Map<dynamic, String>` is true. |
62 Expect.isFalse(f9<int>(<int,int>{})); | 62 Expect.isFalse(f9<int>(<int,int>{})); |
63 | 63 |
64 Expect.isTrue(new IsMap<int>().check<String>(<int,String>{})); | 64 Expect.isTrue(new IsMap<int>().check<String>(<int,String>{})); |
65 Expect.isTrue(new IsMap<int>().check<int>(<int,String>{})); | 65 Expect.isTrue(new IsMap<int>().check<int>(<int,String>{})); |
66 } | 66 } |
OLD | NEW |