Description
Dart SDK Version: 2.0.0-dev.48.0
OS: Windows 10
Dart 2.0 Super Bounded Types Spec reads:
The feature described here, super-bounded types, consists in allowing an actual type argument to be a supertype of the declared bound, as long as a consistent replacement of
Object
,dynamic
, andvoid
byNull
produces a traditional, well-bounded type. For example, if a classC
takes a type argumentX
which must extendC<X>
,C<Object>
,C<dynamic>
, andC<void>
are correct super-bounded types.
We extend covariance for generic class types such that it can be used also in cases where a type argument violates the corresponding bound.
For instance, assuming the classesC
andD
as declared in the Motivation section,C<D>
is a subtype ofC<Object>
. This is new becauseC<Object>
used to be a compile-time error, which means that no questions could be asked about its properties. Note that this is a straightforward application of the usual covariance rule:C<D> <: C<Object>
becauseD <: Object
. We need this relaxation of the rules in order to be able to define which violations of the declared bounds are admissible.
This means that constructions like C<dynamic> c0 = ...
now should be allowed in strong mode for bounded types like class C<X extends C<X>>
.
However, this is not so. For example, the following code sample:
class C<X extends C<X>> {}
class D extends C<D> {}
main() { C<dynamic> c = new D(); }
produces type_argument_not_matching_bounds
error:
$> dartanalyzer --preview-dart-2 test.dart
Analyzing test.dart...
error - 'dynamic' doesn't extend 'C' at test.dart:3:12 - type_argument_not_matching_bounds
hint - The value of the local variable 'c' isn't used at test.dart:3:21 - unused_local_variable
1 error and 1 hint found.