Skip to content

Commit dbacff9

Browse files
committed
wIP
1 parent 36b0dee commit dbacff9

File tree

1 file changed

+13
-7
lines changed

1 file changed

+13
-7
lines changed

working/4213-generic-constructors/feature-specification.md

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,14 @@ class C {
3333
void main() {
3434
C(42); // OK.
3535
C.computed('Hello', (s) => s.length); // OK.
36+
C.computed<String>('Hello', (s) => s.length); // OK.
3637
}
3738
```
3839

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+
3944
The constructor `C.computed` is generic. This can be detected directly from
4045
the syntax because it declares a type parameter list after the second part
4146
of the name, `computed<X>`. This type parameter list can declare any number
@@ -81,9 +86,10 @@ class D<X> {
8186
}
8287
8388
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.
8691
D.ofComparable(C(42), (c1, c2) => c1.i.compareTo(c2.i)); // OK.
92+
D.ofComparable(C(42)); // Compile-time error.
8793
}
8894
```
8995

@@ -112,12 +118,12 @@ class D<X> {
112118
final X x;
113119
final int Function(X, X) _compare;
114120
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>`.
118124
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>`.");
121127
}
122128
}
123129
}

0 commit comments

Comments
 (0)