为 Scala 的枚举派生猫顺序
Deriving a Cats Order for Scala's Enumeration
我想要 Scala Enumeration
的通用 Cats Order
。我试过了
implicit def enumOrder[E <: Enumeration, V <: E#Value]: cats.Order[V] = new cats.Order[V] {
def compare(x: V, y: V): Int = x.compare(y)
}
但我明白了
[error] overloaded method value compare with alternatives:
[error] ((that: _1.Value)Int) forSome { val _1: E } <and>
[error] (that: _1.Value)Int
[error] cannot be applied to (V)
[error] def compare(x: V, y: V): Int = x.compare(y)
[error] ^
有人知道我该如何实现吗?谢谢
注意,我刚刚问了一个 ,我认为它会产生一个我足够聪明的答案来解决这个问题,但它没有。
implicit def enumOrder[V <: Enumeration#Value](implicit ord: Ordering[V]): cats.Order[V] = new cats.Order[V] {
def compare(x: V, y: V): Int = ord.compare(x, y)
}
或
implicit def enumOrder[V <: Enumeration#Value](implicit ord: Ordering[V]): cats.Order[V] = ord.compare(_, _)
我想要 Scala Enumeration
的通用 Cats Order
。我试过了
implicit def enumOrder[E <: Enumeration, V <: E#Value]: cats.Order[V] = new cats.Order[V] {
def compare(x: V, y: V): Int = x.compare(y)
}
但我明白了
[error] overloaded method value compare with alternatives:
[error] ((that: _1.Value)Int) forSome { val _1: E } <and>
[error] (that: _1.Value)Int
[error] cannot be applied to (V)
[error] def compare(x: V, y: V): Int = x.compare(y)
[error] ^
有人知道我该如何实现吗?谢谢
注意,我刚刚问了一个
implicit def enumOrder[V <: Enumeration#Value](implicit ord: Ordering[V]): cats.Order[V] = new cats.Order[V] {
def compare(x: V, y: V): Int = ord.compare(x, y)
}
或
implicit def enumOrder[V <: Enumeration#Value](implicit ord: Ordering[V]): cats.Order[V] = ord.compare(_, _)