我想要Scala的Order
的通用Cats Enumeration
。我试过了
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] ^
有谁知道我怎么能实现这个?谢谢
NB,我刚刚问了一个similar question,我认为这会产生一个答案,我会很聪明地适用于这个问题,但事实并非如此。
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(_, _)