You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
A _type constructor_ is represented internally much like a polymorphic method type.
515
-
`[´\pm´ ´a_1´ >: ´L_1´ <: ´U_1, ..., \pm a_n´ >: ´L_n´ <: ´U_n´] ´T´` represents a type that is expected by a [type constructor parameter](04-basic-declarations-and-definitions.html#type-parameters) or an [abstract type constructor binding](04-basic-declarations-and-definitions.html#type-declarations-and-type-aliases) with the corresponding type parameter clause.
<!-- the definition of a parameterized type above uses the concept of a type constructor, so we can't define a type constructor as an unapplied parameterized type. -->
518
524
519
-
Consider this fragment of the `Iterable[+X]` class:
525
+
A _type constructor_ is either:
526
+
- a _type lambda_, of the form `[´\mathit{tps}\,´] =>> ´T´` where `[´\mathit{tps}\,´]` is a type parameter clause `[´a_1´ >: ´L_1´ <: ´U_1, ..., a_n´ >: ´L_n´ <: ´U_n´]` for some ´n \gt 0´ and ´T´ is either a value type
527
+
or another type lambda.
528
+
- a reference to a [desugared type declaration](04-basic-declarations-and-definitions.html#type-declarations-and-type-aliases) upper-bounded by a type lambda.
529
+
- a reference to a [polymorphic class](05-classes-and-objects.html##class-definitions).
Each type parameter ´a_i´ of a type lambda has a variance ´v_i´ which cannot be written down by the user but is inferred from the body of the type lambda to maximize the number of types that conform to the type lambda.
532
+
<!-- TODO: write down the exact algorithm? -->
526
533
527
-
Conceptually, the type constructor `Iterable` is a name for the anonymous type `[+X] Iterable[X]`, which may be passed to the `newType` type constructor parameter in `flatMap`.
534
+
#### Inferred type parameter clause
535
+
536
+
To each type constructor corresponds an _inferred type parameter clause_ which is computed as follow:
537
+
- For a type lambda, its type parameter clause (including variance annotations).
538
+
- For a type declaration upper-bounded by a type lambda ´T´, the inferred clause of ´T´.
539
+
- For a polymorphic class, its type parameter clause.
528
540
529
541
<!-- ### Overloaded Types
530
542
@@ -660,6 +672,8 @@ We define the following relations between types.
660
672
661
673
### Equivalence
662
674
675
+
´\color{red}{\text{TODO SCALA3: Redefine equivalence as mutual conformance?}}´
676
+
663
677
Equivalence ´(\equiv)´ between types is the smallest congruence [^congruence] such that the following holds:
664
678
665
679
- If ´t´ is defined by a type alias `type ´t´ = ´T´`, then ´t´ is equivalent to ´T´.
@@ -708,7 +722,7 @@ The conformance relation ´(<:)´ is the smallest transitive relation that satis
708
722
- If ´T_i \equiv T_i'´ for ´i \in \{ 1, ..., n\}´ and ´U´ conforms to ´U'´ then the method type ´(p_1:T_1, ..., p_n:T_n) U´ conforms to ´(p_1':T_1', ..., p_n':T_n') U'´.
709
723
- The polymorphic type ´[a_1 >: L_1 <: U_1, ..., a_n >: L_n <: U_n] T´ conforms to the polymorphic type ´[a_1 >: L_1' <: U_1', ..., a_n >: L_n' <: U_n'] T'´ if, assuming ´L_1' <: a_1 <: U_1', ..., L_n' <: a_n <: U_n'´ one has ´T <: T'´ and ´L_i <: L_i'´ and ´U_i' <: U_i´ for ´i \in \{ 1, ..., n \}´.
710
724
- Type constructors ´T´ and ´T'´ follow a similar discipline.
711
-
We characterize ´T´ and ´T'´ by their type parameter clauses ´[a_1, ..., a_n]´ and ´[a_1', ..., a_n']´, where an ´a_i´ or ´a_i'´ may include a variance annotation, a higher-order type parameter clause, and bounds.
725
+
We characterize ´T´ and ´T'´ by their [inferred type parameter clauses](#inferred-type-parameter-clause) ´[a_1, ..., a_n]´ and ´[a_1', ..., a_n']´.
712
726
Then, ´T´ conforms to ´T'´ if any list ´[t_1, ..., t_n]´ -- with declared variances, bounds and higher-order type parameter clauses -- of valid type arguments for ´T'´ is also a valid list of type arguments for ´T´ and ´T[t_1, ..., t_n] <: T'[t_1, ..., t_n]´.
713
727
Note that this entails that:
714
728
- The bounds on ´a_i´ must be weaker than the corresponding bounds declared for ´a'_i´.
Copy file name to clipboardExpand all lines: docs/_spec/04-basic-declarations-and-definitions.md
+48Lines changed: 48 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -226,6 +226,54 @@ Def ::= ‘type’ {nl} TypeDef
226
226
TypeDef ::= id [TypeParamClause] ‘=’ Type
227
227
```
228
228
229
+
### Desugaring of parameterized type declarations
230
+
A parameterized type declaration is desugared into an unparameterized type declaration
231
+
whose bounds are type lambdas with explicit variance annotations.
232
+
233
+
#### Abstract Type
234
+
An abstract type
235
+
```scala
236
+
type ´t´[´\mathit{tps}\,´] >: ´L´ <: ´U´
237
+
```
238
+
is desugared into an unparameterized abstract type as follow:
239
+
- If `L` conforms to `Nothing`, then,
240
+
241
+
```scala
242
+
type ´t´ >:Nothing
243
+
<: [´\mathit{tps'}\,´] =>> ´U´
244
+
```
245
+
- otherwise,
246
+
247
+
```scala
248
+
type ´t´ >: [´\mathit{tps'}\,´] =>> ´L´
249
+
<: [´\mathit{tps'}\,´] =>> ´U´
250
+
```
251
+
252
+
If at least one of the ´\mathit{tps}´ contains an explicit variance annotation, then ´\mathit{tps'} = \mathit{tps}´, otherwise we infer the variance of each type parameter as with the user-written type lambda `[´\mathit{tps}\,´] =>> ´U´`.
253
+
254
+
The same desugaring applies to typeparameters. For instance,
255
+
```scala
256
+
[F[X] <:Coll[X]]
257
+
```
258
+
is treated asa shorthand for
259
+
```scala
260
+
[F>:Nothing<: [X] =>>Coll[X]]
261
+
```
262
+
263
+
####TypeAlias
264
+
A parameterized typealias
265
+
```scala
266
+
type ´t´[´\mathit{tps}\,´] = ´T´
267
+
```
268
+
is desugared into an unparameterized typealias
269
+
```scala
270
+
type ´t´ = [´\mathit{tps'}\,´] =>> ´T´
271
+
```
272
+
where ´\mathit{tps'}´ is computed as in the previous case.
273
+
274
+
´\color{red}{\text{TODOSCALA3:Everythingelse in this section (and the next one
275
+
on typeparameters) needs to be rewritten to take into account the desugaring described above.}}´
276
+
229
277
A _type declaration_ `type ´t´[´\mathit{tps}\,´] >: ´L´ <: ´U´` declares ´t´ to be an abstracttypewith lower bound type ´L´ and upper bound type ´U´.
230
278
If the typeparameter clause `[´\mathit{tps}\,´]` is omitted, ´t´ abstracts over a proper type, otherwise ´t´ stands for a typeconstructor that accepts typeargumentsasdescribed by the typeparameter clause.
0 commit comments