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
-40Lines changed: 0 additions & 40 deletions
Original file line number
Diff line number
Diff line change
@@ -153,46 +153,6 @@ The third rule states that a match type conforms to its upper bound
153
153
154
154
Within a match type `Match(S, Cs) <: B`, all occurrences of type variables count as covariant. By the nature of the cases `Ci` this means that occurrences in pattern position are contravarant (since patterns are represented as function type arguments).
155
155
156
-
## Typing Rules for Match Expressions
157
-
158
-
Typing rules for match expressions are tricky. First, they need some new form of GADT matching for value parameters.
159
-
Second, they have to account for the difference between sequential match on the term level and parallel match on the type level. As a running example consider:
160
-
```scala
161
-
typeM[+X] =Xmatch {
162
-
caseA=>1
163
-
caseB=>2
164
-
}
165
-
```
166
-
We'd like to be able to typecheck
167
-
```scala
168
-
defm[X](x: X):M[X] = x match {
169
-
case_: A=>1// type error
170
-
case_: B=>2// type error
171
-
}
172
-
```
173
-
Unfortunately, this goes nowhere. Let's try the first case. We have: `x.type <: A` and `x.type <: X`. This tells
174
-
us nothing useful about `X`, so we cannot reduce `M` in order to show that the right hand side of the case is valid.
175
-
176
-
The following variant is more promising:
177
-
```scala
178
-
defm(x: Any):M[x.type] = x match {
179
-
case_: A=>1
180
-
case_: B=>2
181
-
}
182
-
```
183
-
To make this work, we'd need a new form of GADT checking: If the scrutinee is a term variable `s`, we can make use of
184
-
the fact that `s.type` must conform to the pattern's type and derive a GADT constraint from that. For the first case above,
185
-
this would be the constraint `x.type <: A`. The new aspect here is that we need GADT constraints over singleton types where
186
-
before we just had constraints over type parameters.
187
-
188
-
Assuming this extension, we can then try to typecheck as usual. E.g. to typecheck the first case `case _: A => 1` of the definition of `m` above, GADT matching will produce the constraint `x.type <: A`. Therefore, `M[x.type]` reduces to the singleton type `1`. The right hand side `1` of the case conforms to this type, so the case typechecks.
189
-
190
-
Typechecking the second case hits a snag, though. In general, the assumption `x.type <: B` is not enough to prove that
191
-
`M[x.type]` reduces to `2`. However we can reduce `M[x.type]` to `2` if the types `A` and `B` do not overlap.
192
-
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))
193
-
194
-
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`.
195
-
196
156
## Overlapping Patterns
197
157
198
158
A complete defininition of when two patterns or types overlap still needs to be worked out. Some examples we want to cover are:
0 commit comments