According to covariance definition:
Q[+B] means that Q can take any class, but if A is a subclass of B, then Q[A] is considered to be a subclass of Q[B].
Let's see the following example:
trait SomeA
trait SomeB extends SomeA
trait SomeC extends SomeB
case class List1[+B](elements: B*)
val a = List1[SomeA](new SomeA{},new SomeB{})
val b = List1[SomeB](new SomeB{},new SomeC{})
Everything is fine, but I don't see why List1[SomeB]
is a subclass of List1[SomeA]
, or in other words why b is a subclass of a
?
Now,
List1[SomeB]
is subclass ofList1[SomeA]
which means you can put the first where the later is needed.If it were invariant that would be not possible, see:
Since all the elements in
List1[SomeB]
are subtype ofSomeA
(asSomeB
extendsSomeA
), you can simply passList1[SomeB]
whereList1[SomeA]
is expected.