scalascala-catsscala-2.12

Deriving a Cats Order for Scala's Enumeration


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.


Solution

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