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
+9-5Lines changed: 9 additions & 5 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -124,17 +124,21 @@ The third rule states that a match type conforms to its upper bound
124
124
125
125
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).
126
126
127
-
## Typing Rules for Match Expressions
127
+
<!-- TODO revise this section, at least `S` has to be invariant according to the current implementation -->
128
+
129
+
## Future Typing Rules for Match Expressions
130
+
131
+
<!-- TODO document the final solution and remove `Future` from `Future Typing Rules...` -->
128
132
129
133
Typing rules for match expressions are tricky. First, they need some new form of GADT matching for value parameters.
130
134
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:
131
135
```scala
132
-
typeM[+X] =Xmatch {
136
+
typeM[X] =Xmatch {
133
137
caseA=>1
134
138
caseB=>2
135
139
}
136
140
```
137
-
We'd like to be able to typecheck
141
+
We would like to be able to typecheck
138
142
```scala
139
143
defm[X](x: X):M[X] = x match {
140
144
case_: A=>1// type error
@@ -144,14 +148,14 @@ def m[X](x: X): M[X] = x match {
144
148
Unfortunately, this goes nowhere. Let's try the first case. We have: `x.type <: A` and `x.type <: X`. This tells
145
149
us nothing useful about `X`, so we cannot reduce `M` in order to show that the right hand side of the case is valid.
146
150
147
-
The following variant is more promising:
151
+
The following variant is more promising but does not compile either:
148
152
```scala
149
153
defm(x: Any):M[x.type] = x match {
150
154
case_: A=>1
151
155
case_: B=>2
152
156
}
153
157
```
154
-
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
158
+
To make this work, we would need a new form of GADT checking: If the scrutinee is a term variable `s`, we can make use of
155
159
the fact that `s.type` must conform to the pattern's type and derive a GADT constraint from that. For the first case above,
156
160
this would be the constraint `x.type <: A`. The new aspect here is that we need GADT constraints over singleton types where
157
161
before we just had constraints over type parameters.
0 commit comments