Question

I'm reading about the context bounds and implicit parameters that are supposed to work like type classes. The examples I see often use Ordering[T]. Something like:

foo[T : Ordering](a: T, b: T)

which is sugar for

foo[T](a: T, b: T)(implicit ord: Ordering[T]) 

so in the body you can do something like

ord.compare(a, b)

I thought I understood this before, and now I'm drawing a blank... what's the advantage over simply passing the thing to the function. For example:

foo[T](a: T, b: T, ord: Ordering[T])
/* or */ foo[T](a: T, b: T)(ord: Ordering[T])
foo(a, b, myOrdering)
/* or */ foo(a, b)(myOrdering)
Was it helpful?

Solution

I try to answer in detail below. The short answer is to not have to pass the implicit values to other method's implicit parameters explicitly.

As I understand you do not mind the syntactic sugar of context bounds and you understand that, so I just skip that part.

Imagine you have two sorting methods, one is sorting the whole array, the other one sorts only a slice of it. They have the following signatures:

sort[T](as: Array[T], ord: Ordering[T]): Unit = sort(as, 0, as.length, ord)
sort[T](as: Array[T], from: Int, to: Int, ord: Ordering[T]): Unit = ???

Now, if there were an Ordering implicit value and there were implicit parameters like this instead:

sort[T](as: Array[T])(implicit ord: Ordering[T]): Unit = sort(as, 0, as.length/*)( ord*/)
sort[T](as: Array[T], from: Int, to: Int)(implicit ord: Ordering[T]): Unit = ???

or with context bounds:

sort[T:Ordering](as: Array[T]): Unit = sort(as, 0, as.length/*)( ord*/)
sort[T:Ordering](as: Array[T], from: Int, to: Int): Unit = ???

See, we did not have to specify the ordering for the general case. You can imagine this with more depth passing of the parameter. In that case the usage is easier, though the declaration without the context bound is a bit more tedious.

(For completeness getting the instance with context bound: implicitly[Ordering[T]], somewhere in the ??? implementation.)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top