Files
sdk/tests/language_2/generic_methods/dynamic_test.dart
T
Leaf Petersen b101a7d002 Add language versions to _2 test libraries
Change-Id: Ib33169c3e0ffc870915c189404074a1dea472546
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/196548
Reviewed-by: Bob Nystrom <rnystrom@google.com>
Commit-Queue: Leaf Petersen <leafp@google.com>
2021-04-26 17:58:57 +00:00

47 lines
1.3 KiB
Dart

// 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.
// @dart = 2.9
// Test that if the type of a parameter of a generic method is a type parameter,
// the type of the passed argument is checked (01) at compile time
// if the receiver is given via an interface-type variable, and (02) at runtime
// if the receiver is dynamic.
library generic_methods_dynamic_test;
import "package:expect/expect.dart";
class A {}
class B {}
class C {
T foo<T>(T t) => t;
List<T> bar<T>(Iterable<T> t) => <T>[t.first];
}
main() {
B b = new B();
C c = new C();
dynamic obj = c;
c.foo<A>(b); //# 01: compile-time error
obj.foo<A>(b); //# 02: runtime error
c.bar<A>(<B>[new B()]); //# 03: compile-time error
obj.bar<A>(<B>[new B()]); //# 04: runtime error
Expect.equals(c.foo<B>(b), b); //# 05: ok
Expect.equals(obj.foo<B>(b), b); //# 05: continued
dynamic x = c.bar<B>(<B>[new B()]); //# 05: continued
Expect.isTrue(x is List<B>); //# 05: continued
Expect.equals(x.length, 1); //# 05: continued
dynamic y = obj.bar<B>(<B>[new B()]); //# 05: continued
Expect.isTrue(y is List<B>); //# 05: continued
Expect.equals(y.length, 1); //# 05: continued
}