【问题标题】:Accessing a Variable at compile time在编译时访问变量
【发布时间】:2021-07-31 19:45:30
【问题描述】:

will trait 提供了对调用它的Variable 的编译时访问。是否有其他方法可以访问将安装在给定词法范围内的Variables? (我知道我可以在运行时访问Scalars,但我正在尝试访问Variables)。

特别是,我希望能够执行以下操作(这是行不通的):

multi trait_mod:<is>(Sub \fn, :$foo) {
    for fn.lexical_variables { #`(do stuff) }
}

有什么办法吗?

【问题讨论】:

  • 我想你可以看看Code@!compstuff属性;但这将非常脆弱,可能会在 newdisp / rakuast 分支中中断。

标签: compilation metaprogramming raku compile-time rakudo


【解决方案1】:

目前没有,但在未来的 Raku 语言版本中应该可以。正在为 Raku 语言(目前称为“RakuAST”)定义标准 AST,并重写编译器前端以使其工作。一旦完成,它将在许多地方暴露出来。宏是最明显的消费者,但它也在计划中:

  • 使块或例程的 AST 可从特征中获得,以便特征可以检查甚至修改 AST
  • 引入自定义编译器通道,这些模块将被授予访问它们所导入范围的整个 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.
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-02-08
    • 1970-01-01
    • 2012-02-23
    • 2019-07-27
    • 2011-08-01
    相关资源
    最近更新 更多