Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to access the symbol table in a macro?

For example, to get all values, and their types, accessible at the macro's call site? Or at least just the values from the current class? E.g.:

class A {
  val v1 = 10
  var v2 = "2"

  def m {
    val m3 = true

    // Here I would like to get information that v1: Int, v2: String and
    // v3: Boolean are available
    macroInvocation() 
  }
}

I looked in the Context and Universe, but couldn't find good methods.

Only solution I found so far is to get the macro's enclosing class/method (via the Context), and search the tree.

like image 464
adamw Avatar asked Nov 03 '22 02:11

adamw


1 Answers

Only solution I found so far is to get the macro's enclosing class/method (via the Context), and search the tree

I have the feeling that you mean that you are actually inspecting the AST itself in order to extract the declared fields. You don't have to do that. If c is your Context value, you can just do:

c.enclosingClass.symbol.typeSignature.members
like image 147
Régis Jean-Gilles Avatar answered Nov 15 '22 05:11

Régis Jean-Gilles