Reputation: 113
I have this piece of code which call the default comparator for Longs
:
val ordering = implicitly[Ordering[Long]]
How can I create my own ordering and call it with implicitly
?
I tried this:
trait MyOrdering extends Ordering[Long] {
override def compare(x: Long, y: Long): Int = x compare y
}
val ordering = implicitly[MyOrdering]
But it doesn't work
Upvotes: 1
Views: 51
Reputation: 51271
You need to put an instance of your new Ordering
implementation into the implicit namespace.
implicit val mo :MyOrdering = new MyOrdering {}
val ordering = implicitly[MyOrdering]
You could also change your trait
to an implicit object
but you'll also need to modify the implicitly
parameter.
implicit object MyOrdering extends Ordering[Long] {
override def compare(x: Long, y: Long): Int = x compare y
}
val ordering = implicitly[MyOrdering.type]
Upvotes: 2