Deriving a Cats Order for Scala's Enumeration

2019-07-08 05:02发布

I would like a generic Cats Order for Scala's Enumeration. I tried

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)
}

but I get

[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]                                      ^

Does anybody know how I can implement this? Thanks

NB, I just asked a similar question, which I thought would yield an answer that I would be smart enough to apply to this question, but it did not.

1条回答
啃猪蹄的小仙女
2楼-- · 2019-07-08 05:19
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)
}

or

implicit def enumOrder[V <: Enumeration#Value](implicit ord: Ordering[V]): cats.Order[V] = ord.compare(_, _)
查看更多
登录 后发表回答