scalameta / scalagen

WIP - Scalameta powered code generation
Apache License 2.0
40 stars 5 forks source link

Consider adding a resolution api #18

Open DavidDudson opened 6 years ago

DavidDudson commented 6 years ago

This would allow cross file resolution.

This would work at the source level only.

The idea here is to allow

// File A
trait SomeTrait {
  def foo: Int = 1 
}
// File B
@DoWorkBasedOnParent
class Foo extends SomeTrait
// File C
case class DoWorkBasedOnParent extends ExtensionGenerator {
    def extend(c: Defn.Class): List[Stat] = {
       // Would have the structure of the given object
       val parents: List[Defn] = c.semanticParents

       // Resolve a Type.Name to a literal type
      val tpe: Option[Tree] = c.type.resolve
    }
}

This idea originally sprung from gitter, but this sort of solution could allow generation of data based on potentially uncontrollable sources. For example auto generating typeclass instances for all classes in the entire scalameta Trees.scala file, without touching the file itself.