OLD | NEW |
(Empty) | |
| 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 |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 import 'package:expect/expect.dart'; |
| 6 |
| 7 void testCallsToGenericFn() { |
| 8 T f<T>(T a, T b) => ((a as dynamic) + b) as T; |
| 9 |
| 10 var x = (f as dynamic)<int>(40, 2); |
| 11 Expect.equals(x, 42); |
| 12 |
| 13 var y = (f as dynamic)<String>('hi', '!'); |
| 14 Expect.equals(y, 'hi!'); |
| 15 |
| 16 var dd2d = (x, y) => x; |
| 17 dd2d = f; // implicit <dynamic> |
| 18 x = (dd2d as dynamic)(40, 2); |
| 19 Expect.equals(x, 42); |
| 20 y = (dd2d as dynamic)('hi', '!'); |
| 21 Expect.equals(y, 'hi!'); |
| 22 } |
| 23 |
| 24 void testGenericFnAsArg() { |
| 25 h<T>(a) => a as T; |
| 26 Object foo(f(Object a), Object a) => f(a); |
| 27 Expect.throws(() => foo(h as dynamic, 42)); |
| 28 |
| 29 var int2int = (int x) => x; |
| 30 T bar<T>(x) => x as T; |
| 31 dynamic list = <Object>[1, 2, 3]; |
| 32 Expect.throws(() => list.map(bar)); |
| 33 int2int = bar; |
| 34 Expect.listEquals(list.map(int2int).toList(), [1, 2, 3]); |
| 35 } |
| 36 |
| 37 typedef T2T = T Function<T>(T t); |
| 38 void testGenericFnAsGenericFnArg() { |
| 39 h<T>(a) => a as T; |
| 40 S foo<S>(T2T f, S a) => f<S>(a); |
| 41 Expect.equals(foo<int>(h, 42), 42); |
| 42 Expect.equals(foo<dynamic>(h, 42), 42); |
| 43 Expect.equals(foo<int>(h as dynamic, 42), 42); |
| 44 Expect.equals(foo<dynamic>(h as dynamic, 42), 42); |
| 45 } |
| 46 |
| 47 void testGenericFnTypeToString() { |
| 48 T f<T>(T a) => a; |
| 49 // TODO(jmesserly): other Dart implementations use `=>` arrow, so we may need |
| 50 // to change this in DDC at some point. |
| 51 Expect.equals(f.runtimeType.toString(), "<T>(T) -> T"); |
| 52 } |
| 53 |
| 54 main() { |
| 55 testCallsToGenericFn(); |
| 56 testGenericFnAsArg(); |
| 57 testGenericFnAsGenericFnArg(); |
| 58 testGenericFnTypeToString(); |
| 59 } |
OLD | NEW |