Skip to content

Commit 0166e5f

Browse files
Remove outdated information from the introduction
The first sentence was wrong, stating that MT reduce to "a number of RHS", whereas the implementation only reduces to a single RHS. This commit also remove the covariant annotation in the Concat example as it's outdated (trying to compile that snippet on master results in "covariant type parameter Xs occurs in invariant position").
1 parent 6eac6be commit 0166e5f

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

docs/docs/reference/new-types/match-types.md

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ layout: doc-page
33
title: "Match Types"
44
---
55

6-
A match type reduces to one of a number of right hand sides, depending on a scrutinee type. Example:
6+
A match type reduces to one of its right-hand sides, depending on a scrutinee type. Example:
77

88
```scala
99
type Elem[X] = X match {
@@ -12,23 +12,29 @@ type Elem[X] = X match {
1212
case Iterable[t] => t
1313
}
1414
```
15-
This defines a type that, depending on the scrutinee type `X`, can reduce to one of its right hand sides. For instance,
15+
16+
This defines a type that reduces as follows:
17+
1618
```scala
1719
Elem[String] =:= Char
1820
Elem[Array[Int]] =:= Int
1921
Elem[List[Float]] =:= Float
2022
Elem[Nil.type] =:= Nothing
2123
```
24+
2225
Here `=:=` is understood to mean that left and right hand sides are mutually subtypes of each other.
2326

2427
In general, a match type is of the form
28+
2529
```scala
2630
S match { P1 => T1 ... Pn => Tn }
2731
```
32+
2833
where `S`, `T1`, ..., `Tn` are types and `P1`, ..., `Pn` are type patterns. Type variables
2934
in patterns start as usual with a lower case letter.
3035

3136
Match types can form part of recursive type definitions. Example:
37+
3238
```scala
3339
type LeafElem[X] = X match {
3440
case String => Char
@@ -37,13 +43,16 @@ type LeafElem[X] = X match {
3743
case AnyVal => X
3844
}
3945
```
46+
4047
Recursive match type definitions can also be given an upper bound, like this:
48+
4149
```scala
42-
type Concat[+Xs <: Tuple, +Ys <: Tuple] <: Tuple = Xs match {
50+
type Concat[Xs <: Tuple, Ys <: Tuple] <: Tuple = Xs match {
4351
case Unit => Ys
4452
case x *: xs => x *: Concat[xs, Ys]
4553
}
4654
```
55+
4756
In this definition, every instance of `Concat[A, B]`, whether reducible or not, is known to be a subtype of `Tuple`. This is necessary to make the recursive invocation `x *: Concat[xs, Ys]` type check, since `*:` demands a `Tuple` as its right operand.
4857

4958
## Representation of Match Types

0 commit comments

Comments
 (0)