I would like to pattern match certain function and other variable references inside a macro traversing over a Tree
. Currently, I am matching based on symbol.fullname
like this:
tree match {
case "$f($x)" if f.symbol != null
&& f.symbol.fullName == "_root_.mypackage.MyClass.myfunc" =>
...
case "$f($x)" if f.symbol != null
&& f.symbol.fullName == "_root_.mypackage.MyClass2.myfunc2" =>
...
case ...
}
However, I would like to additionally check during compilation of this macro that those functions actually exists, e.g. instead of having String, I would like to directly use references, such that the IDE can tell me whether I had a typo in the 'myfunc' name. I was thinking of using using reify
, but I am not sure whether that works.
Now lets assume i was looking for println
instead of MyClass.myfunc
. The first problem I encountered is that you cannot reify(println)
or any other function reference directly, because in Scala there are no function references, so you have to write reify(println _)
or more specificly in this case reify(println (_: String))
to select what println function we want to call. With the following code I collected all symbols that are inside the expression, but sadly only Predef (from Predef.println) is found but not println itself:
println(reify( println (_:String) ).tree
.collect { case x if x.symbol != null => x.symbol.fullName } )
// List(<none>, <none>, <none>, scala.Predef, <none>, <none>, scala.Predef, <none>, <none>, scala.Predef)
Any ideas for getting the name of something in scala (currently using 2.12)?