Frage

Suppose I've got two macro-annotated classes, and after their fields are defined during expansion, one depends on the other:

@Annotation case class A(i: Int) @Annotation case class B(a: A)

I need A to be expanded before B, but the order of their definition does not ensure that.

For example, a reference to B as a type-parameter to an object in an unrelated compilation unit, such as:

class X{val b = B(A(1))} in one file and class Y{Z[B]} in another,

causes B to be expanded first and thus my compilation fails.

What determines the order of macro expansion in this case? Is there any way to enforce a desired order?

Thanks for your input,

Julian

War es hilfreich?

Lösung

typecheck was the solution for me.

If type A (A's tree.tpe) is used in compile-time reflection before its macro has been expanded, it can be expanded as soon as it is stumbled upon:

import c.universe._
import Flag._

def expandType(typeTree: Tree) = {
  c.typecheck(q"type T = $typeTree") match {
    case x @ TypeDef(mods, name, tparams, rhs)  => rhs.tpe
  }
}
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top