gpt4 book ai didi

raku - BEGIN 何时真正启动(或运行)?

转载 作者:行者123 更新时间:2023-12-04 16:57:35 25 4
gpt4 key购买 nike

This is related to this issue in the Perl 6 documentation repo



BEGIN blocks的阶段不太清楚实际上正在运行。文档说“编译时间”,但 Perl 是预编译的,所以可能 actually be precompile time .事实上,让我们使用这段代码

unit module Beginner;

BEGIN {
say "This is where all started";
}

从这里

require Beginner;

say "Begun";

在一个新目录中( rm -rf .precomp 运行)。它输出:

This is where all started
Begun

By now, the .precomp directory that holds the cache has already been created.



所以让我们从这里使用它:

use Beginner;
say "Already started";

它显然只返回 Already started . (如果我们运行第一个程序也会发生同样的情况)。
这可能是一个问题,因为用户可能事先不知道预编译是否会发生,这反过来意味着“编译时间”何时发生是不确定的。无论如何,问题是:我们如何正确地表述这一点以在文档中对其进行解释?另外,应该使用 BEGIN受到鼓励(因为计算出来的值将存储在预编译缓存中,从而有效地从运行时中消除)还是不鼓励?这有什么好的用例吗?

最佳答案

BEGIN发生在编译时,正如您所观察到的那样,这可能是预编译时间。

我认为这没有任何问题,只要您不假设在脚本启动时进行编译。就像在编译时评估 C++ 模板一样,这通常与执行时有很大不同。

Also, should the use of BEGIN be encouraged (since values computed there are going to be stored in the precompilation cache and thus effectively eliminated from runtime) or discouraged



对于适当的用例,应该鼓励一切,并鼓励其他一切。

如果你想在程序启动时运行一些东西,使用 INIT ,不是 BEGIN .

Is there some good use case for this?



许多元编程可以(并且应该)在编译时完成,例如根据固定的名称列表创建方法和属性列表。在每次程序启动时都这样做是一种浪费,而且程序的其他部分可能在编译时需要完整的类型。

关于raku - BEGIN 何时真正启动(或运行)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53839059/

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