2

我想要一个Order用于 Scala 的通用 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]                                      ^

有人知道我该如何实现吗?谢谢

注意,我刚刚问了一个类似的问题,我认为这会产生一个我足够聪明地应用于这个问题的答案,但事实并非如此。

4

1 回答 1

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(_, _)
于 2019-05-01T18:51:09.840 回答