aa2b3a5eaa
Implements https://github.com/dart-lang/language/issues/658. Change-Id: I28857c40bbc5db76f7312ed540052fed40dac4b4 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/125552 Commit-Queue: Kallen Tu <kallentu@google.com> Reviewed-by: Dmitry Stefantsov <dmitryas@google.com> Reviewed-by: Leaf Petersen <leafp@google.com>
54 lines
1.1 KiB
Dart
54 lines
1.1 KiB
Dart
// Copyright (c) 2019, 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.
|
|
|
|
// Tests downwards inference for explicit variance modifiers.
|
|
|
|
// SharedOptions=--enable-experiment=variance
|
|
|
|
class A<out T> {
|
|
final T _x;
|
|
A(T x):_x = x;
|
|
T get x => _x;
|
|
void set x(Object value) {}
|
|
}
|
|
|
|
class B<in T> {
|
|
B(List<T> x);
|
|
void set x(T val) {}
|
|
}
|
|
|
|
class C<out T, S> {
|
|
final T _x;
|
|
C(T x, S y):_x = x;
|
|
T get x => _x;
|
|
void set x(Object value) {}
|
|
void set y(S _value) {}
|
|
}
|
|
|
|
class D<in T> {
|
|
D(T x, void Function(T) y) {}
|
|
void set x(T val) {}
|
|
}
|
|
|
|
main() {
|
|
// int <: T <: Object
|
|
// Choose int
|
|
A<Object> a = new A(3)..x+=1;
|
|
|
|
// int <: T
|
|
// num <: T
|
|
// Choose num
|
|
B<int> b = new B(<num>[])..x=2.2;
|
|
|
|
// int <: T <: Object
|
|
// Choose int
|
|
// int <: S <: Object
|
|
// Choose Object
|
|
C<Object, Object> c = new C(3, 3)..x+=1..y="hello";
|
|
|
|
// int <: T <: num
|
|
// Choose num due to contravariant heuristic.
|
|
D<int> d = new D(3, (num x) {})..x=2.2;
|
|
}
|