Suppose I have this code for extracting the code initialising a variable:
def extractBodyImpl[T: Type](expr: Expr[T])(using Quotes) =
import quotes.reflect._
expr.asTerm.underlyingArgument match
case ident @ Ident(_) =>
ident.symbol.tree match
case ValDef(_,_,rhs) => println(rhs)
case DefDef(_,_,_,rhs) => println(rhs)
'{ () }
inline def extractBody[T](inline expr: T) = ${ extractBodyImpl('expr) }
When called on a variable declared in the same scope it works as desired:
@main def hello() =
val x = 1
extractBody(x)
prints Some(Literal(Constant(1)))
.
However, on a variable from outer scope, it prints None
:
val x = 1
@main def hello() =
extractBody(x)
How can I make it work in the second case?