I have a trait with a member type, and want to have a macro with signature containing this 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 to a static (i.e. non-member) object
or to a macro bundle. But in neither case do I have a foo: Foo
so I could write foo.Bar[A]
.
One workaround I can think of is to use Foo#Bar[A]
and 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 like to avoid it (both because it's not exactly type-safe and because the real case is more complicated). Any alternatives?