OLD | NEW |
(Empty) | |
| 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 |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 // |
| 5 // DartOptions=--generic-method-syntax |
| 6 |
| 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, |
| 9 /// but erased to `dynamic`. |
| 10 |
| 11 library generic_methods_type_expression_test; |
| 12 |
| 13 import "package:expect/expect.dart"; |
| 14 |
| 15 bool f1<T>(Object o) => o is T; |
| 16 |
| 17 bool f2<T>(Object o) => o is List<T>; |
| 18 |
| 19 bool f3<T>(Object o) => o is! T; |
| 20 |
| 21 bool f4<T>(Object o) => o is! List<T>; |
| 22 |
| 23 T f5<T>(Object o) => o as T; |
| 24 |
| 25 List<T> f6<T>(Object o) => o as List<T>; |
| 26 |
| 27 Type f7<T>() => T; |
| 28 |
| 29 class TypeValue<X> { |
| 30 Type get value => X; |
| 31 } |
| 32 |
| 33 Type f8<T>() => new TypeValue<List<T>>().value; |
| 34 |
| 35 main() { |
| 36 String s = "Hello!"; |
| 37 List<String> ss = <String>[s]; |
| 38 Expect.throws(() => f1<int>(42), (e) => e is TypeError); |
| 39 Expect.throws(() => f1<String>(42), (e) => e is TypeError); |
| 40 Expect.equals(f2<int>(<int>[42]), true); |
| 41 Expect.equals(f2<String>(<int>[42]), true); // `is List<dynamic>` is true. |
| 42 Expect.throws(() => f3<int>(42), (e) => e is TypeError); |
| 43 Expect.throws(() => f3<String>(42), (e) => e is TypeError); |
| 44 Expect.equals(f4<int>(<int>[42]), false); |
| 45 Expect.equals(f4<String>(<int>[42]), false); // `is! List<dynamic>` is false. |
| 46 Expect.throws(() => f5<String>(s), (e) => e is TypeError); |
| 47 Expect.throws(() => f5<int>(s), (e) => e is TypeError); |
| 48 Expect.equals(f6<String>(ss), ss); |
| 49 Expect.equals(f6<int>(ss), ss); // `as List<dynamic>` succeeds. |
| 50 Expect.throws(() => f7<int>(), (e) => e is TypeError); |
| 51 Expect.equals(f8<int>(), List); // Returns `List<dynamic>`. |
| 52 } |
OLD | NEW |