cod*_*ons 6 compilation metaprogramming rakudo compile-time raku
该will
特性给人以编译时访问Variable
上,它被称为。还有其他方法可以访问Variable
将安装在给定词法范围内的s 吗?(我知道我可以访问Scalar
在运行时s,但我正在尝试访问Variable
s)。
特别是,我希望能够执行以下操作(不起作用):
multi trait_mod:<is>(Sub \fn, :$foo) {
for fn.lexical_variables { #`(do stuff) }
}
Run Code Online (Sandbox Code Playgroud)
有什么办法吗?
目前还没有,但是在未来的 Raku 语言版本中应该是可能的。正在努力为 Raku 语言(目前称为“RakuAST”)定义标准 AST,并重写编译器前端以使其工作。一旦完成,它将在许多地方公开。宏是最明显的消费者,但它也有计划:
其中第一个似乎可以满足您的用例。继续当前提议的 API,它可能看起来像这样:
multi trait_mod:<is>(Sub $fn, :$foo!) {
for $fn.ast.ast-lexical-declarations {
say "Name: " ~ .lexical-name;
when RakuAST::VarDeclaration::Simple { #`( my $x / state $x / ... ) }
when RakuAST::VarDeclaration::Term { #`( my \x = ... ) }
# Others, depending if you care about parameters, placeholder params, implicits, etc.
}
}
Run Code Online (Sandbox Code Playgroud)