协变covariant[+T] 逆变contravariant[-T]

协变covariant[+T] 逆变contravariant[-T]spark中用的不多,作了解。

 

含义

Scala 标记

协变covariant

C[T’] C[T] 的子类

[+T]

逆变contravariant

C[T] C[T’]的子类

[-T]

不变invariant

C[T] C[T’]无关

[T]

scala> classCovariant[+A]

defined class Covariant

 

scala> val cv:Covariant[AnyRef] = new Covariant[String]

cv: Covariant[AnyRef] =Covariant@4035acf6

 

scala> val cv:Covariant[String] = new Covariant[AnyRef]

<console>:6:error: type mismatch;

 found  : Covariant[AnyRef]

 required: Covariant[String]

       val cv: Covariant[String] = newCovariant[AnyRef]

                                   ^

scala> classContravariant[-A]

defined classContravariant

 

scala> val cv:Contravariant[String] = new Contravariant[AnyRef]

cv:Contravariant[AnyRef] = Contravariant@49fa7ba

 

scala> val fail:Contravariant[AnyRef] = new Contravariant[String]

<console>:6:error: type mismatch;

 found  : Contravariant[String]

 required: Contravariant[AnyRef]

       val fail: Contravariant[AnyRef] = newContravariant[String]

 

 

 

DT大数据微信公众账号:DT_Spark

DT大数据梦工厂scala的所有视频、PPT和代码在百度云盘的链接地址:http://pan.baidu.com/share/home?uk=4013289088&view=share#category/type=0&qq-pf-to=pcqq.group

你可能感兴趣的:(协变covariant[+T] 逆变contravariant[-T])