1

I have

trait X {

}

class Y extends X

Trait A{ def run(x:X){ /////// } }

Class B extends A{ def run(y:Y) }

However scala complains at B's run function

I am confused on how method signature works inheritance. class B should have a method of signature X, but Type Y is a Type X.

Thanks

ZhijieWang
  • 443
  • 1
  • 6
  • 14
  • possible duplicate of [Covariance and Contravariance in Scala, java](http://stackoverflow.com/questions/13321921/covariance-and-contravariance-in-scala-java) – Suma May 22 '15 at 09:14

1 Answers1

3

Methods are contravariant in their argument types. Let me take your example and explain:

val b: B = new B
val y: Y = new Y

b.run(y) // everything is ok

val a: A = b // B is A
val x: X = new X {}

a.run(x) // run on A takes an X. x is of type X
// But: B only takes a Y (more specific type) --> Boom

Let me formulate this in words: In order to support the interface defined in A, B needs to be able to work with any X in its run method. So you cannot know for sure the X it is going to get is also a Y.

Note that you are actually allowed to do the opposite:

class B extends A { def run(x: Any) = /* snip */ }

Since B accepts any value in its run method, it also accepts an X, so everything is fine.

It is interesting to see, that return values are the other way around:

trait A {
  def foo: X
}

class B extends A {
  def foo: Y = /* snip */
}

This is valid, since B just has to return some X. Any Y is also a X, so we are good.

I hope this made it a bit clearer what is going on here and why the compiler forbids you implement your methods that way.

gzm0
  • 14,752
  • 1
  • 36
  • 64
  • following your explanation, if B implements run(Any), then B still needs to implement run(X), because run(x) in class A was abstract. I also tried the revers, define an abstract run(any) in A, but then B will require a run(Any) – ZhijieWang May 22 '15 at 01:17
  • If `B` implements `run(Any)` it already has implemented `run(X)`, since every `X` is also a `Any`. Therefore, the method `run(Any)` is able to handle it as well. – gzm0 May 22 '15 at 16:28