How make implicit Ordered on java.time.LocalDate

前端 未结 5 736
故里飘歌
故里飘歌 2021-01-04 00:33

I want to use java.time.LocalDate and java.time.LocalDateTime with an implicit Ordered like:

val date1 = java.time.Loc         


        
5条回答
  •  孤街浪徒
    2021-01-04 01:23

    A slight modification to the implicit ordered should do the trick.

    type AsComparable[A] = A => Comparable[_ >: A]
    
    implicit def ordered[A: AsComparable]: Ordering[A] = new Ordering[A] {
      def compare(x: A, y: A): Int = x compareTo y
    }
    

    Now every type which is comparable to itself or to a supertype of itself should have an Ordering.

提交回复
热议问题