Alexey Romanov
Alexey Romanov

Reputation: 170899

Implement a macro in superclass and expand it in subclasses

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?

Upvotes: 2

Views: 165

Answers (1)

Eugene Burmako
Eugene Burmako

Reputation: 13048

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.

Upvotes: 2

Related Questions