Using context bound and implicitly in Scala
Posted on August 9, 2016
Context bound is a syntactic sugar for implicit argument:
which is de-sugared to
So far so good, but on the same page there is another very common example in the library:
so one can imagine it’s a syntactic sugar for
Let’s look closer at de-sugared versions:
// Type bound with implicitly
def f(a: Object, b: Object, evidence$2: scala.math.Ordering): Int =
scala.Predef.implicitly(evidence$2).$asInstanceOf[math.Ordering]().compare(a, b);
// Implicit argument
def f(a: Object, b: Object, ev: scala.math.Ordering): Int = ev.compare(a, b);
Well, I have to admit both versions are doing the same thing at the end, but if I’m writing a library method there is a difference. It’s even more obvious when looking at the bytecode:
0: getstatic #32
3: aload_3
4: invokevirtual #36
7: checkcast #14
10: aload_1
11: aload_2
12: invokeinterface #40, 3
17: ireturn
vs
So whenever performance is an issue I would avoid using the pattern with implicitly
.