gpt4 book ai didi

eval - 由于 EVALFILE 引入类符号

转载 作者:行者123 更新时间:2023-12-04 11:18:31 26 4
gpt4 key购买 nike

类名在 EVALFILE 之后的范围内可用,而文档可能另有说明。EVALFILE 的文档states :

Slurps the specified file and evaluates it. Behaves the same way as EVAL with regard to Blob decoding, scoping, the $lang parameter and the $check parameter. Evaluates to the value produced by the final statement in the file when $check is not True.

EVAL 的文档 states :

since the set of symbols in a lexical scope is immutable after compile time, an EVAL can never introduce symbols into the surrounding scope.


我的输入文件是:
class SomeClass {
method a () { "In SomeClass method a"; };
}

sub some-routine () {
"Some routine";
}
> my $f = EVALFILE("/tmp/eval-bug.raku");
&some-routine

> $f()
Some routine

> some-routine()
===SORRY!=== Error while compiling:
Undeclared routine:
some-routine used at line 1
以上三个执行符合文档,但以下不符合:
> SomeClass.a()
In SomeClass method a
那么符号仅表示例程名称而不是类名称吗?或者这是一个错误?

最佳答案

since the set of symbols in a lexical scope is immutable after compile time, an EVAL can never introduce symbols into the surrounding scope


这里的关键词是“词法范围”。然而,类在默认情况下不是词法范围的(与例程不同),而是包范围。这意味着它们默认引入一个全局名称。如果你改为写:
my class SomeClass {
method a () { "In SomeClass method a"; };
}
然后这声明了一个词法范围的类,不会引入全局名称,因此它不会安装在任何超出 EVALFILE 的地方。 .
至少对我来说,文档似乎是正确的;它明确提到词法范围,而“周围范围”一词也带有文本(因此是词法)范围界定的含义。然而,它可能会提到在 EVALFILE 中声明的包范围的东西。将在全局安装。

关于eval - 由于 EVALFILE 引入类符号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68903534/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com