# Scala教程之:深入理解协变和逆变

04/11 20:54

### 文章目录

``````sealed abstract class List[+A] ... { // 忽略了混入的trait
...
def map[B](f: A => B): List[B] = {...}
...
}``````

## 函数的参数和返回值

``````trait Function1[-T1, +R] extends AnyRef { self =>
/** Apply the body of this function to the argument.
*  @return   the result of function application.
*/
def apply(v1: T1): R

...

override def toString() = "<function1>"
}``````

``````scala> var f: Int=>Int = i=>i+1
f: Int => Int = <function1>``````

``````val f: Int => Int = new Function1[Int,Int] {
def apply(i: Int): Int = i + 1
}``````

``````class CSuper { def msuper() = println("CSuper") }
class C extends CSuper { def m() = println("C") }
class CSub extends C { def msub() = println("CSub") }``````

``````var f: C => C = (c: C) => new C // ➋
f = (c: CSuper) => new CSub // ➌
f = (c: CSuper) => new C // ➍
f = (c: C) => new CSub // ➎
f = (c: CSub) => new CSuper // ➏ 编译错误!``````

``def apply(i: CSub): CSuper = new CSuper``

CSub=>CSuper就是f的运行类型。

``````scala> trait MyFunction2[+T1, +T2, -R] {
| def apply(v1:T1, v2:T2): R = ???
| }
<console>:37: error: contravariant type R occurs in covariant position
in type (v1: T1, v2: T2)R of method apply
def apply(v1:T1, v2:T2): R = ???
^
<console>:37: error: covariant type T1 occurs in contravariant position
in type T1 of value v1
def apply(v1:T1, v2:T2): R = ???
^
<console>:37: error: covariant type T2 occurs in contravariant position
in type T2 of value v2
def apply(v1:T1, v2:T2): R = ???
^``````

## 可变类型的变异

``````scala> class ContainerPlus[+A](var value: A)
<console>:34: error: covariant type A occurs in contravariant position
in type A of value value_=
class ContainerPlus[+A](var value: A)
^
scala> class ContainerMinus[-A](var value: A)
<console>:34: error: contravariant type A occurs in covariant position
in type => A of method value
class ContainerMinus[-A](var value: A)``````

``val cp: ContainerPlus[C]=new ContainerPlus(new CSub)``

``val cm: ContainerMinus[C] = new ContainerMinus(new CSuper)``

1
0 收藏

0 评论
0 收藏
1