使用协变泛型实现 Ordered trait
Implement Ordered trait with covariant generic
有没有办法混合协变泛型的 Ordered 特征?
我有以下代码:
trait Foo[+T <: Foo[T]] extends Ordered[T] {
def id: Int
override def compare(that : T) : Int = {
this.id compare that.id
}
}
我需要 T
协变并且也希望能正常工作。上面的版本给出了 "covariant type in contravariant position error"。
Ordered[A]
在 A
中不变。 The old documentation for this trait explains why:
A trait for totally ordered data. Note that since version 2006-07-24 this trait is no longer covariant in a. It is important that the equals method for an instance of Ordered[A] be consistent with the compare method. However, due to limitations inherent in the type erasure semantics, there is no reasonable way to provide a default implementation of equality for instances of Ordered[A]. Therefore, if you need to be able to use equality on an instance of Ordered[A] you must provide it yourself either when inheiriting or instantiating. It is important that the hashCode method for an instance of Ordered[A] be consistent with the compare method. However, it is not possible to provide a sensible default implementation. Therefore, if you need to be able compute the hash of an instance of Ordered[A] you must provide it yourself either when inheiriting or instantiating.
这意味着如果您想使用 Ordered[A]
,您必须明确地为 Foo
.
的子类型提供 compare
的实现
可以使用隐式 Ordering[A]
:
解决方法
implicit def ord[A <: Foo[A]] = new math.Ordering[A] {
override def compare(a: A, b: A) = a.id compare b.id
}
您不能将 Ordered
与协变类型一起使用,因为它需要泛型处于逆变位置。相反,您应该使用在伴随对象
中定义的隐式 Ordering
trait Foo[+T] {
def id: Int
}
object Foo {
implicit def fooOrdering[A <: Foo[_]]: Ordering[A] = {
new Ordering[A] {
override def compare(x: A, y: A): Int = x.id compare y.id
}
}
}
任何比较对象的合理函数都应该为它正在比较的对象接受一个 Ordering 实例,而且很多都是隐式的。例如
case class F(id: Int) extends Foo[Int]
case class G(id: Int) extends Foo[Int]
List(F(1), F(2), F(5), F(3), G(12)).max // = G(12)
有没有办法混合协变泛型的 Ordered 特征?
我有以下代码:
trait Foo[+T <: Foo[T]] extends Ordered[T] {
def id: Int
override def compare(that : T) : Int = {
this.id compare that.id
}
}
我需要 T
协变并且也希望能正常工作。上面的版本给出了 "covariant type in contravariant position error"。
Ordered[A]
在 A
中不变。 The old documentation for this trait explains why:
A trait for totally ordered data. Note that since version 2006-07-24 this trait is no longer covariant in a. It is important that the equals method for an instance of Ordered[A] be consistent with the compare method. However, due to limitations inherent in the type erasure semantics, there is no reasonable way to provide a default implementation of equality for instances of Ordered[A]. Therefore, if you need to be able to use equality on an instance of Ordered[A] you must provide it yourself either when inheiriting or instantiating. It is important that the hashCode method for an instance of Ordered[A] be consistent with the compare method. However, it is not possible to provide a sensible default implementation. Therefore, if you need to be able compute the hash of an instance of Ordered[A] you must provide it yourself either when inheiriting or instantiating.
这意味着如果您想使用 Ordered[A]
,您必须明确地为 Foo
.
compare
的实现
可以使用隐式 Ordering[A]
:
implicit def ord[A <: Foo[A]] = new math.Ordering[A] {
override def compare(a: A, b: A) = a.id compare b.id
}
您不能将 Ordered
与协变类型一起使用,因为它需要泛型处于逆变位置。相反,您应该使用在伴随对象
Ordering
trait Foo[+T] {
def id: Int
}
object Foo {
implicit def fooOrdering[A <: Foo[_]]: Ordering[A] = {
new Ordering[A] {
override def compare(x: A, y: A): Int = x.id compare y.id
}
}
}
任何比较对象的合理函数都应该为它正在比较的对象接受一个 Ordering 实例,而且很多都是隐式的。例如
case class F(id: Int) extends Foo[Int]
case class G(id: Int) extends Foo[Int]
List(F(1), F(2), F(5), F(3), G(12)).max // = G(12)