Scala 泛型类型扩展了 Comparable 接口
Scala generics type extends Comparable interface
以下 Scala 代码工作正常:
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))
但是接下来编译出错:
val bc1 = new C[B](b1)
val bc2 = new C[B](b2)
println(bc1.compareTo(bc2))
错误:类型参数 [B] 不符合 class C 的类型参数范围 [T <: Comparable[T]]
实际上class B 也有一个compareTo 成员。如何更改 class C 的定义以使其与 class B 兼容?谢谢!
因为 B
扩展了 Comparable[A]
,而不是 Comparable[B]
。它与 T <: Comparable[T]
不匹配(因为 B <: Comparable[B]
不正确)。
以下代码工作正常:
val bc1 = new C[A](b1)
val bc2 = new C[A](b2)
println(bc1.compareTo(bc2))
考虑像这样使用 Ordering
类型 class 的替代实施方式
class A(val value: Int)
class B(value: Int) extends A(value)
class C[T](val value: T)
implicit val aOrdering = new Ordering[A] {
def compare(a1: A, a2: A): Int = a1.value compare a2.value
}
implicit val bOrdering = new Ordering[B] {
def compare(b1: B, b2: B): Int = b1.value compare b2.value
}
implicit class CompareToA[T <: A](`this`: T) {
def compareTo(that: T)(implicit o: Ordering[T]): Int = o.compare(`this`, that)
}
implicit class CompareToC[T](`this`: C[T]) {
def compareTo(that: C[T])(implicit o: Ordering[T]): Int = o.compare(`this`.value, that.value)
}
输出
val b1 = new B(1)
val b2 = new B(2)
val bc1 = new C[B](b1)
val bc2 = new C[B](b2)
bc1.compareTo(bc2) // res3: Int = -1
以下 Scala 代码工作正常:
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))
但是接下来编译出错:
val bc1 = new C[B](b1)
val bc2 = new C[B](b2)
println(bc1.compareTo(bc2))
错误:类型参数 [B] 不符合 class C 的类型参数范围 [T <: Comparable[T]]
实际上class B 也有一个compareTo 成员。如何更改 class C 的定义以使其与 class B 兼容?谢谢!
因为 B
扩展了 Comparable[A]
,而不是 Comparable[B]
。它与 T <: Comparable[T]
不匹配(因为 B <: Comparable[B]
不正确)。
以下代码工作正常:
val bc1 = new C[A](b1)
val bc2 = new C[A](b2)
println(bc1.compareTo(bc2))
考虑像这样使用 Ordering
类型 class 的替代实施方式
class A(val value: Int)
class B(value: Int) extends A(value)
class C[T](val value: T)
implicit val aOrdering = new Ordering[A] {
def compare(a1: A, a2: A): Int = a1.value compare a2.value
}
implicit val bOrdering = new Ordering[B] {
def compare(b1: B, b2: B): Int = b1.value compare b2.value
}
implicit class CompareToA[T <: A](`this`: T) {
def compareTo(that: T)(implicit o: Ordering[T]): Int = o.compare(`this`, that)
}
implicit class CompareToC[T](`this`: C[T]) {
def compareTo(that: C[T])(implicit o: Ordering[T]): Int = o.compare(`this`.value, that.value)
}
输出
val b1 = new B(1)
val b2 = new B(2)
val bc1 = new C[B](b1)
val bc2 = new C[B](b2)
bc1.compareTo(bc2) // res3: Int = -1