Using Scala macros I would like to get access to source code of function f.
Here is simplified example of my problem:
def logFImplementation(f: => Boolean) {
val sourceCodeOfF: String = ... // <-- how to get source code of f??
println("Scala source of f=" + sourceCodeOfF)
}
so for:
logFImplementation { val i = 5; List(1, 2, 3); true }
it should print:
Scala source of f=val i: Int = 5; immutable.this.List.apply[Int](1, 2, 3); true
(Right now I tested Macro to access source code text at runtime and it works great for { val i = 5; List(1, 2, 3); true }.logValueImpl
but for f.logValueImpl
it just prints f
.)
Thanks for any advice.
This is perhaps 'thread necromancy' as the question is quite old. I've had to grapple with this a bit.
An example of it in use (has to be in a separate compilation module) is:
I seriously doubt that it is possible.
First, macros are compile-time thing. All they do is transform abstract syntax trees your program consists of. That's exactly the reason why you were able to do
{ val i = 5; List(1, 2, 3); true }.logValueImpl
: AST is right here, so there is no difficulty printing it as string. But macros just can't resolve runtime values, because there are no macros at runtime.Second, there is absolutely no reason to believe that
f
is Scala code. I could call your function from Java like this (not sure that I got all names right, but the idea is still the same):(and this is possible because internally call-by-name parameters are transformed into
scala.Function0
objects). What would you expect your function to print in this case?