Scala macros referring to a member type -
i have trait member type, , want have macro signature containing type:
trait foo { class bar[a] { ... } def baz[a](x: bar[a]): bar[a] = macro bazimpl[a] def bazimpl[a: c.weaktypetag](c: blackbox.context)(x: c.expr[bar[a]]) = ... }
this doesn't work, since bazimpl
must belong either static (i.e. non-member) object
or macro bundle. in neither case have foo: foo
write foo.bar[a]
.
one workaround can think of use foo#bar[a]
, add casts:
trait foo { class bar[a] { ... } def baz[a](x: bar[a]): bar[a] = foo.baz1(x).asinstanceof[bar[a]] def bazimpl[a: c.weaktypetag](c: blackbox.context)(x: c.expr[bar[a]]) = ... } object foo { def baz1[a](x: foo#bar[a]): foo#bar[a] = macro bazimpl[a] def bazimpl[a: c.weaktypetag](c: blackbox.context)(x: c.expr[foo#bar[a]]): c.expr[foo#bar[a]] = ... }
but i'd avoid (both because it's not type-safe , because real case more complicated). alternatives?
if you're using scala 2.11, write c.tree
everywhere instead of c.expr
. cut down boilerplate of specifying same types in macro impls in macro defs @ virtually 0 cost. potential downside of trees in comparison exprs reify, requires splicees exprs, since of metaprograms in scala written quasiquotes these days, chances won't matter you.
Comments
Post a Comment