I have this phase:
class ExpPhase extends PluginPhase {
import tpd._
val phaseName = "expPhase"
override val runsAfter = Set("typer")
override def transformTypeDef(tree: TypeDef)(implicit ctx: Context): Tree =
if tree.isClassDef && !tree.rhs.symbol.isStatic then // only look at classes & traits, not objects
val c2 = QuotesCache.init(ctx.fresh)
implicit val quotes = scala.quoted.runtime.impl.QuotesImpl()(using c2)
import quotes.reflect.*
// Add new parent, SJ_Serialize, so class Person becomes class Person extends SJ_Serialize
val cd = ClassDef.copy(classDef)(
name = classDef.name,
constr = classDef.constructor,
parents = classDef.parents :+ TypeTree.of[SJ_Serialize], // SJ_Serialize is a trait having methods
selfOpt = classDef.self,
body = classDef.body
)
val newCd = cd.asInstanceOf[dotty.tools.dotc.ast.tpd.Tree]
println(QuotesImpl.showDecompiledTree(newCd)) // see if class modified successfully
newCd
else
tree
}
When I look at the printed class (newCd) I see that it shows as now mixing in SJ_Serialize. Great! But not so great...
This phase happens early in the flow. I see that programmer-written mixin traits have their methods copied into the target class at some point later in the flow. I expected that to happen with my new added trait, but it did not. (SJ_Serialize has implemented methods that should be callable on the target class after mixing in.)
Makes me wonder if there's other machinery I need to connect to make this parent addition work properly?
What else am I missing to make this work, and how would I actually do it?