@@ -147,22 +147,38 @@ where `C` is an identifier or prefixed identifier that denotes a class,
147
147
mixin, enum, or extension type declaration, we say that the _ on-class_
148
148
of ` E ` is ` C ` , and the _ constructor return type_ of ` E ` is ` C<T1 .. Tk> ` .
149
149
150
+ In an extension of the form ` extension E on F<T1 .. Tk> {...} ` where ` F ` is
151
+ a type alias whose transitive alias expansion denotes a class, mixin, enum,
152
+ or extension type ` C ` , we say that the _ on-class_ of ` E ` is ` C ` , and the
153
+ _ constructor return type_ of ` E ` is the transitive alias expansion of
154
+ ` F<T1 .. Tk> ` .
155
+
156
+ For the purpose of identifying the on-class and constructor return type of
157
+ a given extension, the types ` void ` , ` dynamic ` , and ` Never ` are not
158
+ considered to be classes, and neither are record types or function types.
159
+
160
+ * Also note that none of the following types are classes:*
161
+
162
+ - * A type of the form ` T? ` or ` FutureOr<T> ` , for any type ` T ` .*
163
+ - * A type variable.*
164
+ - * An intersection type* .
165
+
150
166
In all other cases, an extension declaration does not have an on-class nor a
151
167
constructor return type.
152
168
153
- * For example, an extension whose on-type is a type variable does not have
154
- an on-class, and neither does an extension whose on-type is a function
155
- type .*
169
+ * It may well be possible to allow record types and function types to be
170
+ extended with constructors that are declared in an extension, but this is
171
+ left as a potential future enhancement .*
156
172
157
173
### Static Analysis
158
174
159
175
At first, we establish some sanity requirements for an extension declaration
160
176
by specifying several errors.
161
177
162
- A compile-time error occurs if an extension has an on-clause of the
163
- form ` on C ` where ` C ` denotes a generic class and no type arguments are
164
- passed to ` C ` * (i.e., it is a raw type) * , and the extension contains
165
- one or more constructor declarations .
178
+ It is a compile-time error to declare a constructor in an extension whose
179
+ on-type is not regular-bounded, assuming that the type parameters declared
180
+ by the extension satisfy their bounds. It is a compile-time error to invoke
181
+ a constructor of an extension whose on-type is not regular-bounded .
166
182
167
183
Tools may report diagnostic messages like warnings or lints in certain
168
184
situations. This is not part of the specification, but here is one
@@ -199,8 +215,16 @@ If `C` contains such a declaration then the expression is an invocation of
199
215
that static member of ` C ` , with the same static analysis and dynamic
200
216
semantics as before the introduction of this feature.
201
217
218
+ Otherwise, if ` C ` declares a constructor named ` C.m ` then the given
219
+ expression is not a static member invocation. It is then handled with the
220
+ same static analysis and dynamic semantics as before the introduction of
221
+ this feature * (it may be an error, or it may be an instance creation
222
+ expression; in any case, declarations in ` C ` are given a higher priority
223
+ than declarations in extensions)* .
224
+
202
225
Otherwise, an error occurs if no declarations named ` m ` or more than one
203
- declaration named ` m ` were found. * They would necessarily be declared in
226
+ declaration named ` m ` were found, or if both static members named ` m ` and
227
+ constructors named ` C.m ` were found. * They would necessarily be declared in
204
228
extensions.*
205
229
206
230
Otherwise, the invocation is resolved to the given static member
@@ -210,34 +234,26 @@ above)*.
210
234
211
235
#### The instantiated constructor return type of an extension
212
236
213
- We associate an extension declaration _ D_ named ` E ` with formal type
214
- parameters ` X1 extends B1 .. Xs extends Bs ` and an actual type argument
215
- list ` T1 .. Ts ` with a type known as the _ instantiated constructor return
216
- type of_ _ D_ _ with type arguments_ ` T1 .. Ts ` .
217
-
218
- When an extension declaration _ D_ named ` E ` has an on-clause which denotes
219
- a non-generic class ` C ` , the instantiated constructor return type is ` C ` ,
220
- for any list of actual type arguments.
221
-
222
- * It is not very useful to declare a type parameter of an extension
223
- which isn't used in the constructor return type, because it can only be
224
- passed in an explicitly resolved constructor invocation, e.g.,
225
- ` E<int>.C(42) ` . In all other invocations, the value of such type variables
226
- is determined by instantiation to bound. In any case, the type parameters
227
- are always ignored by static member declarations, they are only relevant to
228
- constructors and instance members.*
229
-
230
- When an extension declaration _ D_ has no formal type parameters, and
231
- it has an on-type ` C<S1 .. Sk> ` , the instantiated constructor return type
232
- of _ D_ is ` C<S1 .. Sk> ` . * In this case the on-type is a fixed type (also
233
- known as a ground type), e.g., ` List<int> ` . This implies that the
234
- constructor return type of D is the same for every call site.*
235
-
236
- Finally we have the general case: Consider an extension declaration
237
- _ D_ named ` E ` with formal type parameters ` X1 extends B1 .. Xs extends Bs `
238
- and a constructor return type ` C<S1 .. Sk> ` . With actual type arguments
239
- ` T1 .. Ts ` , the instantiated constructor return type of _ D_ with type
240
- arguments ` T1 .. Ts ` is ` [T1/X1 .. Ts/Xs]C<S1 .. Sk> ` .
237
+ Assume that _ D_ is a generic extension declaration named ` E ` with formal
238
+ type parameters ` X1 extends B1, ..., Xs extends Bs ` and constructor return
239
+ type ` C<S1 .. Sk> ` . Let ` T1, ..., Ts ` be a list of types. The
240
+ _ instantiated constructor return type_ of _ D_ _ with actual type arguments_
241
+ ` T1 .. Ts ` is then the type ` [T1/X1 .. Ts/Xs]C<S1 .. Sk> ` .
242
+
243
+ * As a special case, assume that _ D_ has an on-clause which denotes a
244
+ non-generic class ` C ` . In this case, the instantiated constructor return
245
+ type is ` C ` , for any list of actual type arguments.*
246
+
247
+ * It is not very useful to declare a type parameter of an extension which
248
+ isn't used in the on-type (and hence in the constructor return type, if it
249
+ exists), because it can only be passed in an explicitly resolved
250
+ invocation, e.g., ` E<int>.C(42) ` . In all other invocations, the value of
251
+ such type variables is determined by instantiation to bound.*
252
+
253
+ * As another special case, assume that _ D_ has no formal type parameters,
254
+ and it has a constructor return type of the form ` C<S1 .. Sk> ` . In this
255
+ case the instantiated constructor return type of _ D_ is ` C<S1 .. Sk> ` ,
256
+ which is a ground type, and it is the same for all call sites.*
241
257
242
258
#### Explicit invocation of a constructor in an extension
243
259
0 commit comments