class C [+T] {
var v : T = _
}
compiler error: covariant type T occurs in contravariant position in type T of value value_=
why? how can I fix it?
You can declare it as private[this]
:
class C [+T] { private[this] var v : T = _ }
Any usage you try that this scope does not allow would be unsafe with a co-variant T
.
You cannot have a var of a covariant type.
A var amounts to having, among other things, a public def v_=(newV: T)
, so it makes T appears as a routine argument, which is a contravariant position. So you must either
To be a little more verbose on the "why" part of your question, by making T is covariant parameter with +T, you states that you wants C[B] to a a subtype of C[A] if B is a subtype of A. This means you want to allows :
val cb: C[B] = new C[B]
val ca : C[A] = cb
To makes this sounds, the compiler restricts where T may appear in C. To make it short and with minor simplifications, v cannot appear as parameter of a routine (or as the type of a var). Otherwise, after you have initialized cb and ca as described above you coulddo
ca.v = new A
This would be allowed, as ca
is supposed to be a C[A]
, so its variable v
is of type A
. However, as C is covariant in T, ca
may (and does in the example) reference a C[B]
instance. Would this assignment be allowed, you could then do
val vInCb: B = cb.v
confident that this gives you a B. However, you just put an A there through the ca
reference. This situation must be forbidden, and it is, by forbidding covariant type parameter T as the type of a var.
You have to make it a val
. A var
always has a setter method where the type appears in contravariant position.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With