Why is Function[-A1,…,+B] not about allowing any supertypes as parameters?

前端 未结 5 1547
别那么骄傲
别那么骄傲 2020-12-01 00:00

I believe one can define covariance (at least, for objects) as \'the ability to use a value of a narrower (sub) type in place of a value of some wider (super) type\', and th

5条回答
  •  佛祖请我去吃肉
    2020-12-01 00:30

    A simplified explanation

    class A
    class B extends A
    val printA: A => Unit = { a => println("Blah blah blah") }
    printA(new A())   //"Blah blah blah"
    printA(new B())   //"Blah blah blah"
    

    contravariance rule:

    If B is a subtype of A, then printA[A] is a subtype of printA[B]

    Since printA[B] is the superclass, we can use printA(new B())

提交回复
热议问题