// Copyright (c) 2020, 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. import "package:expect/expect.dart"; // Tests that inference can solve for T? ~ dynamic or void. class C { final T? value; const C(this.value); C.list(List list) : value = list.first; void set nonNullValue(T value) {} } List foo(T? value) => [value!]; List bar(List value) => [ for (var element in value) if (element != null) element ]; extension Ext on List { List whereNotNull() => [ for (var element in this) if (element != null) element ]; } main() { { // Testing for dynamic. const dynamic o = 42; var c = const C(o); var f = foo(o); var l = [o].whereNotNull(); c.expectStaticType>>(); Expect.type>(c); // Run-time type is subtype of C. c.nonNullValue = Object(); // And supertype. f.expectStaticType>>(); Expect.type>(f); // Run-time type is subtype of List. f[0] = Object(); // And supertype. l.expectStaticType>>(); Expect.type>(l); // Run-time type is subtype of List. l[0] = Object(); // And supertype. } { // Testing for void List o = [42]; var c = C.list(o); var f = bar(o); var l = o.whereNotNull(); c.expectStaticType>>(); Expect.type>(c); // Run-time type is subtype of C. c.nonNullValue = Object(); // And supertype. f.expectStaticType>>(); Expect.type>(f); // Run-time type is subtype of List. f[0] = Object(); // And supertype. l.expectStaticType>>(); Expect.type>(l); // Run-time type is subtype of List. l[0] = Object(); // And supertype. } } // Captures and checks static type of expression. extension TypeCheck on T { T expectStaticType>() { return this; } } typedef Exactly = T Function(T);