3 回答

TA貢獻1848條經驗 獲得超6個贊
trait List[+A]
List[Int]
List[AnyVal]
Int
AnyVal
List[Int]
List[AnyVal]
Object[] arr = new Integer[1];arr[0] = "Hello, there!";
String
Integer[]
ArrayStoreException
Array
[A]
[+A]
).
trait Function1[-P, +R] { def apply(p: P): R}
P
Function1
P
R
T1' <: T1 T2 <: T2'---------------------------------------- S-FunFunction1[T1, T2] <: Function1[T1', T2']
T1'
T1
T2
T2'
函數 A是另一個函數的子類型。 B如果參數類型為 A的參數類型的超級類型。 B的返回類型 A的返回類型的子類型。 B.
trait List[+A] { def cons(hd: A): List[A]}
A
cons
A
List
A
List[A]
cons
A
cons
A
def cons[B >: A](v: B): List[B]
A
B
A
A
A
List
B
List
B
A

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.get
Animal
Dog
添加回答
舉報