[英]Expand type class operations on elements using macros
假設以下設置:
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) }
在a
是適當的隱式A
,是否可以使用宏將m op n
直接擴展為a.op(m,n)
,或者至少避免創建其他對象?
如果將隱式參數移至op
方法,則可以使用值類來防止其他對象的創建:
implicit class some2E[L](val self: L) extends AnyVal {
def op(other: L)(implicit x: A[L]) = x.op(self, other)
}
熱點可能會內聯對some2E
定義的op
的調用,因此您最終將得到a.op(m, n)
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.