Question

I want to do something like this:

trait Endo {
  def apply[T](x: T): T
}

trait SuperType {
  def endoMap(f: Endo): SuperType = macro SuperTypeMacro.endoMapImpl
}

case class Foo(x: Int) extends SuperType {
  // endoMapImpl expands to
  // Foo(f(x))
}

case class Bar(x: Int, y: Boolean) extends SuperType {
  // endoMapImpl expands to
  // Bar(f(x), f(y))
}

Ideally the only thing I would need to write is extends SuperType. Is it possible? If not, I believe macro annotations should allow this; am I right?

Was it helpful?

Solution

I think what you're looking for is c.prefix, that contains the receiver of the current macro method call. Once you get hold of it, you can branch on its type.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top