I may be using the wrong terminology, but here is an example of what I would like to achieve. Suppose I have the following macro:
def generateField[T]: AnyRef =
macro generateFieldImpl[T]
def generateFieldImpl[T: c.AbsTypeTag](c: Context): c.Expr[AnyRef] = {
/**
* here I'm looking at the type T by reflection to see now many members it has
* and based on that I'm generating TupleN[Array[Byte], ...](null, ...)
* where N is number of members in class represented by type T
*/
}
I plan to use only case classes like T.
When I use this macro with the case class, it works fine, but now I would like to add an abstraction layer:
trait WithGeneratedField[T] {
val _myField = generateField[T]
}
The problem that I am facing is that the macro expands when the attribute is declared and at this point T is called the abstract type "T". Is there any way to postpone macrodecomposition until I confuse this trait with something specific? For instance:
case class MyClass(a: String, b: Int) extends WithGeneratedField[MyClass]
In the end, my goal is to use a macro to add the generated field to the case class. Maybe there is a better way to do this?