I have a question about Scala variances.
The code below is valid code, which passes compile.
// <Code A>
// VALID (COMPILE PASS!)
class A[+T, -U](t: T, u: U)
But the code below is not valid, which use val
and doesn't pass compile.
// <Code B>
// INVALID (COMPILE ERROR)
class A[+T, -U](val t: T, val u: U)
The error message is the following.
error: contravariant type U occurs in covariant position in type => U of value u
class A[+T, -U](val t: T, val u: U)
^
I wonder why <Code A>
is valid and <Code B>
isn't valid. Could someone tell me the reason?