The following scala code works fine:
class A(val value : Int) extends Comparable[A]
{
override def compareTo(o: A): Int = this.value.compareTo(o.value)
}
class B(value : Int) extends A(value)
class C[T <: Comparable[T]](val value : T) extends Comparable[C[T]]
{
override def compareTo(o: C[T]): Int = value.compareTo(o.value)
}
val a1 = new A(1)
val a2 = new A(2)
println(a1.compareTo(a2))
val b1 = new B(1)
val b2 = new B(2)
println(b1.compareTo(b2))
val ac1 = new C[A](a1)
val ac2 = new C[A](a2)
println(ac1.compareTo(ac2))
But the next compiles with error:
val bc1 = new C[B](b1)
val bc2 = new C[B](b2)
println(bc1.compareTo(bc2))
Error: type arguments [B] do not conform to class C's type parameter bounds [T <: Comparable[T]]
Actually class B has also a compareTo member. How can I change class C's definition to make it compatible with class B? Thanks!