Accepted answer

This is occurring because, in class C, while you know U is a subtype of B, and T is a subtype of A, you are not guaranteeing that U is a subtype of T.

EDIT: I think I've found a way to do this. It uses generalised type constraints:

abstract class C[T <: A, U <: B] {

  def addTwo(t: T): Int = t.a + 2

  def fail(u: U)(implicit ev: U <:< T): Int = addTwo(u)  // compiles


I haven't done a full test of this, though.


I don't know what C is modelling, but I would have said:

scala> class C[T <: A, U <: B] { def f(t: T) = t.a ; def g[X <: U with T](x: X) = f(x) }
defined class C

That is, you're just trying to say that whatever T and U are, the thing you pass to g is both.

Like the other answer says, where you put the constraints depends on what you're trying to do.

Related Query

More Query from same tag