Assuming the following setup:
trait A[L] { def op(l1:L, l2:L): L }
trait E[L] { def op(l:L): L }
implicit def some2E[L:A](self:L) = new E[L] { def op(other:L) =
implicitly[A[L]].op(self,other) }
Is there a way to directly expand m op n
to a.op(m,n)
, in a context where a
is the appropriate implicit A
, using macros or at least avoid the additional object creation?
If you move the implicit parameter to the op
method, you can use a value class to prevent the additional object creation:
implicit class some2E[L](val self: L) extends AnyVal {
def op(other: L)(implicit x: A[L]) = x.op(self, other)
}
Hotspot will probably inline the call to the op
defined in some2E
, so you will end up with a.op(m, n)
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.