Here's some code from this tutorial:
case class ListNode[+T](h: T, t: ListNode[T]) {
def head: T = h
def tail: ListNode[T] = t
def prepend(elem: T): ListNode[T] =
ListNode(elem, this)
}
The tutorial says:
Unfortunately, this program does not compile, because a covariance annotation is only possible if the type variable is used only in covariant positions. Since type variable T appears as a parameter type of method prepend, this rule is broken.
How is T
not in a covariant position in predend
, and the other T
references (def head: T = h
and def tail: ListNode[T] = t
), apparently, are covariant?
What I'm asking is why T
in prepend
is not covariant. This is certainly not covered in Why is Function[-A1,...,+B] not about allowing any supertypes as parameters?, which seems to be what others have directed me to read.