3 回答
TA贡献1848条经验 获得超6个赞
trait List[+A]
List[Int]List[AnyVal]IntAnyValList[Int]List[AnyVal]
Object[] arr = new Integer[1];arr[0] = "Hello, there!";
StringInteger[]ArrayStoreExceptionArray[A][+A]).
trait Function1[-P, +R] {
def apply(p: P): R}PFunction1PR
T1' <: T1 T2 <: T2'---------------------------------------- S-FunFunction1[T1, T2] <: Function1[T1', T2']
T1'T1T2T2'
函数 A是另一个函数的子类型。 B如果参数类型为 A的参数类型的超级类型。 B的返回类型 A的返回类型的子类型。 B.
trait List[+A] {
def cons(hd: A): List[A]}AconsAListAList[A]cons
AconsA
def cons[B >: A](v: B): List[B]
ABAAA
ListBListBA
TA贡献1893条经验 获得超10个赞
class Slot[+T](var some: T) {
def get: T = some
}
val slot: Slot[Dog] = new Slot[Dog](new Dog)
val slot2: Slot[Animal] = slot //because of co-variance
slot2.some = new Animal //legal as some is a var
slot.get ??slot.getAnimalDog
添加回答
举报
