为什么变量不在“constant”声明中插入?

Jim*_*ger 5 constants string-interpolation raku

use v6.d;

my Str $foo = 'Hello';
my constant $BAR = "--$foo--";
say $BAR;
Run Code Online (Sandbox Code Playgroud)

输出:

Use of uninitialized value element of type Str in string context.
Methods .^name, .raku, .gist, or .say can be used to stringify it to something meaningful.
  in block  at deleteme.raku line 4
----
Run Code Online (Sandbox Code Playgroud)

预期输出:

--Hello--
Run Code Online (Sandbox Code Playgroud)

my没有或 代替our也会发生同样的事情my

[187] > $*DISTRO
macos (12.6)
[188] > $*KERNEL
darwin
[189] > $*RAKU
Raku (6.d)
Run Code Online (Sandbox Code Playgroud)

Jon*_*ton 10

分配给 a 的值constant是在编译时而不是运行时计算的。这意味着常量值可以作为编译的一部分进行计算并缓存。

常规分配发生在运行时。因此,在:

my Str $foo = 'Hello';
my constant $BAR = "--$foo--";
Run Code Online (Sandbox Code Playgroud)

评估$foo当时尚未发生的分配。"--$foo--"相比之下,如果$foo是常量,则该值在编译时可用并进行插值,因此:

my constant $foo = 'Hello';
my constant $BAR = "--$foo--";
say $BAR;
Run Code Online (Sandbox Code Playgroud)

生产:

--Hello--
Run Code Online (Sandbox Code Playgroud)