// Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file // for details. All rights reserved. Use of this source code is governed by a // BSD-style license that can be found in the LICENSE file. // Formatting can break multitests, so don't format them. // dart format off import 'package:expect/expect.dart'; void testCallsToGenericFn() { T f(T a, T b) => ((a as dynamic) + b) as T; var x = (f as dynamic)(40, 2); Expect.equals(x, 42); var y = (f as dynamic)('hi', '!'); Expect.equals(y, 'hi!'); var dd2d = (x, y) => x; dd2d = f; // implicit x = (dd2d as dynamic)(40, 2); Expect.equals(x, 42); y = (dd2d as dynamic)('hi', '!'); Expect.equals(y, 'hi!'); } void testGenericFnAsArg() { h(a) => a as T; Object foo(f(Object a), Object a) => f(a); Expect.throws(() => foo(h as dynamic, 42)); var int2int = (int x) => x; T bar(x) => x as T; dynamic list = [1, 2, 3]; Expect.throws(() => list.map(bar)); int2int = bar; Expect.listEquals([1, 2, 3], list.map(int2int).toList()); } typedef T2T = T Function(T t); void testGenericFnAsGenericFnArg() { h(a) => a as T; S foo(T2T f, S a) => f(a); Expect.equals(foo(h, 42), 42); Expect.equals(foo(h, 42), 42); Expect.equals(foo(h as dynamic, 42), 42); Expect.equals(foo(h as dynamic, 42), 42); } void testGenericFnTypeToString() { T f(T a) => a; Expect.isTrue( f.runtimeType.toString().contains(RegExp(r'<(\w+)>\(\1\) => \1'))); } main() { testCallsToGenericFn(); testGenericFnAsArg(); testGenericFnAsGenericFnArg(); testGenericFnTypeToString(); //# 01: ok }