Given:
class Invar[T]
trait ExtendsAnyref extends AnyRef
def f(a: Invar[ExtendsAnyref]) = {}
The following is erroneous
scala> val x: Function1[Invar[_ <: AnyRef], Unit] = f
<console>:13: error: type mismatch;
found : Invar[ExtendsAnyref] => Unit
required: Invar[_ <: AnyRef] => Unit
val x: Function1[Invar[_ <: AnyRef], Unit] = f
^
Why?
I understand that in Scala, generic types have by
default nonvariant subtyping. Thus, in the context of this example, instances of Invar
with different type parameters would never be in a subtype relationship with each other. So an Invar[ExtendsAnyref]
would not be usable as a Invar[AnyRef]
.
But I am confused about the meaning of _ <: AnyRef
which I understood to mean "some type below AnyRef
in the type hierarchy." ExtendsAnyref
is some type below AnyRef
in the type hierarchy, so I would expect Invar[ExtendsAnyref]
to conform to Invar[_ <: AnyRef]
.
I understand that function objects are contravariant in their input-parameter types, but since I use Invar[_ <: AnyRef]
rather than Invar[AnyRef]
I understood, apparently incorrectly, the use of the upper bounds would have the meaning "Invar
parameterized with Anyref
or any extension thereof."
What am I missing?