|
| 1 | +// All three All type positions in a match type (scrutinee, patterns, bodies) |
| 2 | +// are considered invariant, as showed by the following examples: |
| 3 | + |
| 4 | +trait TA[+Plus] { type M[X] = Plus match { case Int => String } } // error |
| 5 | +// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 6 | +// covariant type Plus occurs in invariant position in type [X] = |
| 7 | +// Plus match { |
| 8 | +// case Int => String |
| 9 | +// } of type M |
| 10 | + |
| 11 | +trait TB[+Plus] { type M[X] = X match { case Plus => String } } // error |
| 12 | +// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 13 | +// covariant type Plus occurs in invariant position in type [X] = |
| 14 | +// X match { |
| 15 | +// case Plus => String |
| 16 | +// } of type M |
| 17 | + |
| 18 | +trait TC[+Plus] { type M[X] = X match { case Int => Plus } } // error |
| 19 | +// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 20 | +// covariant type Plus occurs in invariant position in type [X] = |
| 21 | +// X match { |
| 22 | +// case Int => Plus |
| 23 | +// } of type M |
| 24 | + |
| 25 | +trait TD[-Minus] { type M[X] = Minus match { case Int => String } } // error |
| 26 | +// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 27 | +// contravariant type Minus occurs in invariant position in type [X] = |
| 28 | +// Minus match { |
| 29 | +// case Int => String |
| 30 | +// } of type M |
| 31 | + |
| 32 | +trait TE[-Minus] { type M[X] = X match { case Minus => String } } // error |
| 33 | +// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 34 | +// contravariant type Minus occurs in invariant position in type [X] = |
| 35 | +// X match { |
| 36 | +// case Minus => String |
| 37 | +// } of type M |
| 38 | + |
| 39 | +trait TF[-Minus] { type M[X] = X match { case Int => Minus } } // error |
| 40 | +// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 41 | +// contravariant type Minus occurs in invariant position in type [X] = |
| 42 | +// X match { |
| 43 | +// case Int => Minus |
| 44 | +// } of type M |
| 45 | + |
| 46 | +// Furthermore, both unannotated type parameters and unannotated type bindings |
| 47 | +// in patterns are invariant, as showed by the following examples: |
| 48 | + |
| 49 | +trait Cov[+X] |
| 50 | +trait Contra[-X] |
| 51 | + |
| 52 | +// As usual: |
| 53 | +type Test0[X] = Cov[X] // OK |
| 54 | +type Test1[X] = Contra[X] // OK |
| 55 | +type Test2[+X] = Contra[X] // error: covariant type parameter X occurs in |
| 56 | + // in contravariant position in Contra[X] |
| 57 | +type Test3[-X] = Cov[X] // error: contravariant type parameter X occurs in |
| 58 | + // covariant position in Cov[X] |
| 59 | + |
| 60 | +type M0[X] = X match { case Int => Cov[X] } |
| 61 | +type M1[X] = X match { case Int => Contra[X] } |
| 62 | +type M2[X] = X match { case Cov[x] => Contra[x] } |
| 63 | +type M3[X] = X match { case Contra[x] => Cov[x] } |
0 commit comments