@@ -33,9 +33,14 @@ class C {
33
33
void main() {
34
34
C(42); // OK.
35
35
C.computed('Hello', (s) => s.length); // OK.
36
+ C.computed<String>('Hello', (s) => s.length); // OK.
36
37
}
37
38
```
38
39
40
+ Note that a constructor named ` C ` can be designated as ` C.new ` , which can
41
+ be used to declare and also to pass actual type arguments. So it is not
42
+ required for a generic constructor to have a name of the form ` C.name ` .
43
+
39
44
The constructor ` C.computed ` is generic. This can be detected directly from
40
45
the syntax because it declares a type parameter list after the second part
41
46
of the name, ` computed<X> ` . This type parameter list can declare any number
@@ -81,9 +86,10 @@ class D<X> {
81
86
}
82
87
83
88
void main() {
84
- D.ofComparable(1); // OK, creates a `D<num>` because `num <: Comparable<num>`.
85
- D.ofComparable<num>(1); // It is also OK to pass the type argument explicitly .
89
+ D.ofComparable(1); // OK, type argument `num <: Comparable<num>`.
90
+ D.ofComparable<num>(1); // Also OK .
86
91
D.ofComparable(C(42), (c1, c2) => c1.i.compareTo(c2.i)); // OK.
92
+ D.ofComparable(C(42)); // Compile-time error.
87
93
}
88
94
```
89
95
@@ -112,12 +118,12 @@ class D<X> {
112
118
final X x;
113
119
final int Function(X, X) _compare;
114
120
D(this.x, this._compare);
115
- D.ofComparable(X x):
116
- this(x, (dynamic x1, dynamic x2) => x1.compareTo(x2)) { // Unsafe!
117
- // It is possible to check at run-time that `X extends Comparable<X>`.
121
+ D.ofComparable(X x): // Unsafe!
122
+ this(x, (dynamic x1, dynamic x2) => x1.compareTo(x2)) {
123
+ // Check at run-time that `X extends Comparable<X>`.
118
124
if (<X>[] is! List<Comparable<X>>) {
119
- throw ArgumentError(
120
- "The type argument failed to satisfy `X extends Comparable<X>`.");
125
+ throw ArgumentError("The type argument failed"
126
+ " to satisfy `X extends Comparable<X>`.");
121
127
}
122
128
}
123
129
}
0 commit comments