英文:
Using "is default" with Hash of Arrays
问题
这是一个可工作的示例:
```lang-raku
my %hash;
for 1..4 -> $i {
%hash{$i} = Array.new without %hash{$i};
%hash{$i}.push: $_ for ^$i;
}
say %hash; # 输出:{1 => [0], 2 => [0 1], 3 => [0 1 2], 4 => [0 1 2 3]}
但是为什么下一个类似的示例不起作用?
my %hash is default(Array.new);
for 1..4 -> $i {
%hash{$i}.push: $_ for ^$i;
}
say %hash; # 输出:{}
这甚至更让我困惑,因为下一个示例按预期工作:
my %hash is default(42);
for 1..4 -> $i {
%hash{$i}.=Str;
}
say %hash.raku; # 输出:{"1" => "42", "2" => "42", "3" => "42", "4" => "42"}
<details>
<summary>英文:</summary>
Here is the working example:
```lang-raku
my %hash;
for 1..4 -> $i {
%hash{$i} = Array.new without %hash{$i};
%hash{$i}.push: $_ for ^$i;
}
say %hash; # OUTPUT: {1 => [0], 2 => [0 1], 3 => [0 1 2], 4 => [0 1 2 3]}
But why the next similar example doesn't work?
my %hash is default(Array.new);
for 1..4 -> $i {
%hash{$i}.push: $_ for ^$i;
}
say %hash; # OUTPUT: {}
This even more confuses me, because the next example works as expected:
my %hash is default(42);
for 1..4 -> $i {
%hash{$i}.=Str;
}
say %hash.raku; # OUTPUT: {"1" => "42", "2" => "42", "3" => "42", "4" => "42"}
答案1
得分: 12
不清楚第二个例子的结果为空哈希对我来说并不立即明显,然而像这样使用 is default
不会按您期望的那样起作用。特质在编译时应用;因此,即使 is default(Array.new)
如果正确运行,也会在编译时创建一个单独的 Array
实例,并在全局范围内重用它。因此,我期望的输出是类似于:
1 => [0 0 1 0 1 2 0 1 2 3], 2 => [0 0 1 0 1 2 0 1 2 3], 3 => [0 0 1 0 1 2 0 1 2 3], 4 => [0 0 1 0 1 2 0 1 2 3]}
它没有提供这个结果可能是一个错误。
然而,由于自动实现,第一个例子可以简化为:
my %hash;
for 1..4 -> $i {
%hash{$i}.push: $_ for ^$i;
}
say %hash; # {1 => [0], 2 => [0 1], 3 => [0 1 2], 4 => [0 1 2 3]}
在对未定义的值执行数组操作时,数组会自动创建,因此在这种情况下不需要使用 is default
。
英文:
It's not immediately clear to me why the result of the second example is an empty hash, however using is default
like this is not going to work as you wish. Traits are applied at compile time; thus is default(Array.new)
, even if it worked correctly, would create a single Array
instance at compile time, and reuse it globally. So the output I'd expect is something like:
1 => [0 0 1 0 1 2 0 1 2 3], 2 => [0 0 1 0 1 2 0 1 2 3], 3 => [0 0 1 0 1 2 0 1 2 3], 4 => [0 0 1 0 1 2 0 1 2 3]}
That it doesn't give this is probably a bug.
However, thanks to auto-vivification, the first example can be reduced to:
my %hash;
for 1..4 -> $i {
%hash{$i}.push: $_ for ^$i;
}
say %hash; # {1 => [0], 2 => [0 1], 3 => [0 1 2], 4 => [0 1 2 3]}
The array is created automatically when doing an array operation on an undefined value anyway, thus meaning there's no use for is default
in this kind of situation.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论