0

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?

Greg
  • 10,696
  • 22
  • 68
  • 98
  • The beginning: https://stackoverflow.com/questions/74712087/how-can-i-override-a-class-method-in-a-scala-3-compiler-plugin – Dmytro Mitin Dec 09 '22 at 18:23
  • Not exactly—these two questions are closely related but not the same (I think…). This question is how to add a new mixin and have the mixin’s methods successfully propagate to the children. The question in the link is how to actually override a method of an existing mixin. I actually need to figure out both, but as I worked with it, decided to start with basic mixin first. – Greg Dec 09 '22 at 21:54
  • I just added the link for the reference. – Dmytro Mitin Dec 10 '22 at 03:20

0 Answers0