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/new-types/match-types.md
+10-10Lines changed: 10 additions & 10 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -74,7 +74,7 @@ if `Ci = [Xs] => P => T` and there are minimal instantiations `Is` of the type v
74
74
T' = [Xs := Is] T
75
75
```
76
76
An instantiation `Is` is _minimal_ for `Xs` if all type variables in `Xs` that appear
77
-
covariantly and nonvariantly in `Is` are as small as possible and all type variables in `Xs` that appear contravariantly in `Is` are as large as possible. Here, "small" and "large" are understood wrt`<:`.
77
+
covariantly and nonvariantly in `Is` are as small as possible and all type variables in `Xs` that appear contravariantly in `Is` are as large as possible. Here, "small" and "large" are understood with respect to `<:`.
78
78
79
79
For simplicity, we have omitted constraint handling so far. The full formulation of subtyping tests describes them as a function from a constraint and a pair of types to
80
80
either _success_ and a new constraint or _failure_. In the context of reduction, the subtyping test `S <: [Xs := Is] P` is understood to leave the bounds of all variables
@@ -88,20 +88,20 @@ if
88
88
```
89
89
Match(S, C1, ..., Cn) can-reduce i, T
90
90
```
91
-
and, for `j` in `1..i-1`: `C_j` is disjoint from `C_i`, or else `S` cannot possibly match `C_j`.
92
-
See the section on overlapping patterns for an elaboration of "disjoint" and "cannot possibly match".
91
+
and, for `j` in `1..i-1`: `Cj` is disjoint from `Ci`, or else `S` cannot possibly match `Cj`.
92
+
See the section on [overlapping patterns](#overlapping-patterns) for an elaboration of "disjoint" and "cannot possibly match".
93
93
94
94
## Subtyping Rules for Match Types
95
95
96
96
The following rules apply to match types. For simplicity, we omit environments and constraints.
97
97
98
98
The first rule is a structural comparison between two match types:
99
99
```
100
-
Match(S, C1, ..., Cn) <: Match(T, D1, ..., Dm)
100
+
Match(S, C1, ..., Cm) <: Match(T, D1, ..., Dn)
101
101
```
102
102
``if
103
103
```
104
-
S <: T, m <= n, Ci <: Di for i in 1..n
104
+
S <: T, m >= n, Ci <: Di for i in 1..n
105
105
```
106
106
I.e. scrutinees and corresponding cases must be subtypes, no case re-ordering is allowed, but the subtype can have more cases than the supertype.
107
107
@@ -160,7 +160,7 @@ Assuming this extension, we can then try to typecheck as usual. E.g. to typechec
160
160
161
161
Typechecking the second case hits a snag, though. In general, the assumption `x.type <: B` is not enough to prove that
162
162
`M[x.type]` reduces to `2`. However we can reduce `M[x.type]` to `2` if the types `A` and `B` do not overlap.
163
-
So correspondence of match terms to match types is feasible only in the case of non-overlapping patterns.
163
+
So correspondence of match terms to match types is feasible only in the case of non-overlapping patterns (see next section about [overlapping patterns](#overlapping-patterns))
164
164
165
165
For simplicity, we have disregarded the `null` value in this discussion. `null` does not cause a fundamental problem but complicates things somewhat because some forms of patterns do not match `null`.
166
166
@@ -172,10 +172,10 @@ A complete defininition of when two patterns or types overlap still needs to be
172
172
- A final class `C` overlaps with a trait `T` only if `C` extends `T` directly or indirectly.
173
173
- A class overlaps with a sealed trait `T` only if it overlaps with one of the known subclasses of `T`.
174
174
- An abstract type or type parameter `A` overlaps with a type `B` only if `A`'s upper bound overlaps with `B`.
175
-
- A union type `A_1 | A_2` overlaps with `B` only if `A_1` overlaps with `B` or `A_2` overlaps with `B`.
176
-
- An intersection type `A_1 & A_2` overlaps with `B` only if both `A_1` and `A_2` overlap with `B`.
177
-
- If `C[X_1, ..., X_n]` is a case class, then the instance type `C[A_1, ..., A_n]` overlaps with the instance type `C[B_1, ..., B_n]` only if for every index `i` in `1..n`,
178
-
if `X_i` is the type of a parameter of the class, then `A_i` overlaps with `B_i`.
175
+
- A union type `A1 | ... | An` overlaps with `B` only if at least one of `Ai` for `i` in `1..n` overlaps with `B`.
176
+
- An intersection type `A1 & ... & An` overlaps with `B` only if all of `Ai` for `i` in `1..n` overlap with `B`.
177
+
- If `C[X1, ..., Xn]` is a case class, then the instance type `C[A1, ..., An]` overlaps with the instance type `C[B1, ..., Bn]` only if for every index `i` in `1..n`,
178
+
if `Xi` is the type of a parameter of the class, then `Ai` overlaps with `Bi`.
179
179
180
180
The last rule in particular is needed to detect non-overlaps for cases where the scrutinee and the patterns are tuples. I.e. `(Int, String)` does not overlap `(Int, Int)` since
0 commit comments