Scala - Covariance

妖精的绣舞 提交于 2019-12-13 01:59:12

问题


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?


回答1:


Now, List1[SomeB] is subclass of List1[SomeA] which means you can put the first where the later is needed.

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))

If it were invariant that would be not possible, see:

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))



回答2:


Since all the elements in List1[SomeB] are subtype of SomeA (as SomeB extends SomeA), you can simply pass List1[SomeB] where List1[SomeA] is expected.



来源:https://stackoverflow.com/questions/36935441/scala-covariance

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!