0

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?

bnord
  • 385
  • 1
  • 12

1 Answers1

0

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 theop defined in some2E, so you will end up with a.op(m, n).

wingedsubmariner
  • 13,350
  • 1
  • 27
  • 52
  • Thanks, I came up with this also. Performance wise this brings me half way to writing `a.op(m,n)` directly for non-primitive `L` for primitie `L` it's still a factor of 20 slower probably because there is a lot of boxing going on in the bytecode. – bnord Jun 04 '14 at 09:17