Files
sdk/tests/language_2/variance/variance_in_field_test.dart
T
Kallen Tu 9461df2084 Language tests for variance modifiers.
These test basic correct usages of variance modifiers in fields
and methods.

Erroneous usages will come in a future CL, as well as more complex
examples + subclassing.


Change-Id: I05d547ac1db9e22235a0bf64b05fcb3e119e3157
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/119169
Commit-Queue: Kallen Tu <kallentu@google.com>
Reviewed-by: Leaf Petersen <leafp@google.com>
Reviewed-by: Erik Ernst <eernst@google.com>
2019-10-05 01:18:42 +00:00

57 lines
984 B
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 various fields for the `in` variance modifier.
// SharedOptions=--enable-experiment=variance
import "package:expect/expect.dart";
typedef Int2Void = void Function(int);
class A<in T> {
void set a(T value) => value;
final void Function(T) b = (T val) {
Expect.equals(2, val);
};
A<T> get c => this;
}
mixin BMixin<in T> {
void set a(T value) => value;
final void Function(T) b = (T val) {
Expect.equals(2, val);
};
BMixin<T> get c => this;
}
class B with BMixin<int> {}
void testClass() {
A<int> a = new A();
a.a = 2;
Expect.type<Int2Void>(a.b);
a.b(2);
a.c.a = 2;
}
void testMixin() {
B b = new B();
b.a = 2;
Expect.type<Int2Void>(b.b);
b.b(2);
b.c.a = 2;
}
main() {
testClass();
testMixin();
}