得出Cat订单以进行Scala枚举

时间:2019-05-01 18:23:32

标签: scala scala-cats scala-2.12

我想为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,我认为这会产生一个答案,我很聪明可以应用于这个问题,但事实并非如此。

1 个答案:

答案 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(_, _)