我想为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] ^
有人知道我该怎么实现吗?谢谢
请注意,我只是问了一个similar question,我认为这会产生一个答案,我很聪明可以应用于这个问题,但事实并非如此。
答案 0 :(得分:3)
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(_, _)