Deriving a Cats Order for Scala's Enumeration

那年仲夏 提交于 2019-12-10 23:46:06

问题


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:


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


来源:https://stackoverflow.com/questions/55940830/deriving-a-cats-order-for-scalas-enumeration

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!