これは、後者が必要な場所に最初のものを配置できることを意味するList1[SomeB]
サブクラスです。List1[SomeA]
scala> case class List1[+B](elements: B*)
scala> val a = List1[SomeA](new SomeA{},new SomeB{})
a: List1[SomeA] = List1(WrappedArray($anon$2@3e48e859, $anon$1@31ddd4a4))
scala> val b = List1[SomeB](new SomeB{},new SomeC{})
b: List1[SomeB] = List1(WrappedArray($anon$2@5e8c34a0, $anon$1@7c1c5936))
scala> val c: List1[SomeA] = b
c: List1[SomeA] = List1(WrappedArray($anon$2@5e8c34a0, $anon$1@7c1c5936))
scala> val c: List1[SomeA] = a
c: List1[SomeA] = List1(WrappedArray($anon$2@3e48e859, $anon$1@31ddd4a4))
不可能なことが不変である場合は、以下を参照してください。
scala> case class List1[B](elements: B*)
defined class List1
scala> val c: List1[SomeA] = b
<console>:16: error: type mismatch;
found : List1[SomeB]
required: List1[SomeA]
Note: SomeB <: SomeA, but class List1 is invariant in type B.
You may wish to define B as +B instead. (SLS 4.5)
val c: List1[SomeA] = b
^
scala> val c: List1[SomeA] = a
c: List1[SomeA] = List1(WrappedArray($anon$2@45acdd11, $anon$1@3f0d6038))