Skip to content

Commit

Permalink
Add cats Order->stdlib Ordering implicit summoner to implicits, insta…
Browse files Browse the repository at this point in the history
…nces.order (#1670)
  • Loading branch information
edmundnoble authored Jul 6, 2017
1 parent 99b543b commit 73d180b
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 1 deletion.
3 changes: 2 additions & 1 deletion core/src/main/scala/cats/instances/order.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package instances

import cats.functor.Contravariant

trait OrderInstances {
trait OrderInstances extends cats.kernel.OrderToOrderingConversion {

implicit val catsFunctorContravariantForOrder: Contravariant[Order] =
new Contravariant[Order] {
Expand All @@ -14,3 +14,4 @@ trait OrderInstances {
def contramap[A, B](fa: Order[A])(f: B => A): Order[B] = fa.on(f)
}
}

9 changes: 9 additions & 0 deletions kernel/src/main/scala/cats/kernel/Order.scala
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,15 @@ abstract class OrderFunctions[O[T] <: Order[T]] extends PartialOrderFunctions[O]
ev.max(x, y)
}

trait OrderToOrderingConversion {
/**
* Implicitly derive a `scala.math.Ordering[A]` from a `Order[A]`
* instance.
*/
implicit def catsKernelOrderingForOrder[A](implicit ev: Order[A]): Ordering[A] =
ev.toOrdering
}

object Order extends OrderFunctions[Order] {
/**
* Access an implicit `Order[A]`.
Expand Down
14 changes: 14 additions & 0 deletions tests/src/test/scala/cats/tests/OrderTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,18 @@ class OrderTests extends FunSuite {
Invariant[Order]
Contravariant[Order]
}

// ambiguity test:
// the Ordering instance from the Order instance should be trumped
// by the one provided in the Ordering companion object
{
import cats.instances.all._
Ordering[String]
class C
implicit val ording: Ordering[C] = new Ordering[C] {
def compare(x: C, y: C) = 0
}
implicit val ord: Order[C] = Order.allEqual
Ordering[C]
}
}

0 comments on commit 73d180b

Please sign in to comment.