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
Copy file name to clipboardExpand all lines: docs/docs/reference/type-lambdas-spec.md
+30-14Lines changed: 30 additions & 14 deletions
Original file line number
Diff line number
Diff line change
@@ -19,22 +19,24 @@ If a parameter is is bounded, as in `[X >: L <: H] => F[X]` it is checked that a
19
19
Only the upper bound `H` can be F-bounded, i.e. `X` can appear in it.
20
20
21
21
A variance annotation on a parameter indicates a subtyping relationship on type instances. For instance, given
22
-
```
22
+
```scala
23
23
typeTL1= [+A] =>F[A]
24
24
typeTL2= [-A] =>F[A]
25
25
```
26
26
and two types `S <: T`, we have
27
-
```
28
-
TL1[S] <: TL1[T
27
+
```scala
28
+
TL1[S] <:TL1[T]
29
29
TL2[T] <:TL2[S]
30
30
```
31
-
It is checked that variance annotations on parameters of type lambdas are respected by the parameter occurrences on the
32
-
type lambda's body.
31
+
It is checked that variance annotations on parameters of type lambdas are respected by the parameter occurrences on the type lambda's body.
32
+
33
+
**Note** No requirements hold for the variances of occurrences of type variables in their bounds. It is an open question whether we need to impose additional requirements here
34
+
(`scalac` doesn't check variances in bounds either).
33
35
34
36
## Subtyping Rules
35
37
36
38
Assume two type lambdas
37
-
```
39
+
```scala
38
40
typeTL1= [v1 X>:L1<:U1] =>R1
39
41
typeTL2= [v2 X>:L2<:U2] =>R2
40
42
```
@@ -45,41 +47,55 @@ Then `TL1 <: TL2`, if the type interval `L2..U2` is contained in the type interv
45
47
## Relationship with Parameterized Type Definitions
46
48
47
49
A parameterized type definition
48
-
```
50
+
```scala
49
51
typeT[X] =R
50
52
```
51
53
is regarded as a shorthand for an unparameterized definition with a type lambda as right-hand side:
52
-
```
54
+
```scala
53
55
typeT= [X] =>R
54
56
```
55
57
56
58
A parameterized abstract type
57
-
```
59
+
```scala
58
60
typeT[X] >:L<:U
59
61
```
60
62
is regarded as shorthand for an unparameterized abstract type with type lambdas as bounds.
61
-
```
63
+
```scala
62
64
typeT>: ([X] =>L) <: ([X] =>U)
63
65
```
64
66
However, if `L` is `Nothing` it is not parameterized, since `Nothing` is treated as a bottom type for all kinds. For instance,
65
-
```
67
+
```scala
66
68
typeT[-X] <:X=> ()
67
69
```
68
70
is expanded to
69
-
```
71
+
```scala
70
72
typeT>:Nothing<: ([-X] =>X=> ())
71
73
```
72
74
instead of
73
-
```
75
+
```scala
74
76
typeT>: ([X] =>Nothing) <: ([-X] =>X=> ())
75
77
```
76
78
79
+
The same expansions apply to type parameters. E.g.
80
+
```scala
81
+
[F[X] <:Coll[X]]
82
+
```
83
+
is treated as a shorthand for
84
+
```scala
85
+
[F>:Nothing<: [X] =>Coll[X]]
86
+
```
87
+
77
88
**Note**: The decision to treat `Nothing` as universal bottom type is provisional, and might be changed afer further discussion.
78
89
79
90
**Note**: Scala 2 and 3 differ in that Scala 2 also treats `Any` as universal top-type. This is not done in Scala 3. See also the discussion on [kind polymorphism](./kind-polymorphism.html)
80
91
92
+
## Curried Type Parameters
81
93
82
-
94
+
The body of a type lambda can again be a type lambda. Example:
95
+
```scala
96
+
typeTL= [X] => [Y] => (X, Y)
97
+
```
98
+
Currently, no special provision is made to infer type arguments to such curried type lambdas. This is left for future work.
0 commit comments