在perl6中,我可以创建一个固定大小的数组:my @array [5];
然后我怎样才能将数组的大小更改为更大,例如我希望数组的大小为7
谢谢
In perl6 I can create an array of fixed size as so: my @array[5];
How can I then change the size of the array later on to be larger, for instance I want array to be of size 7 now
Thanks
最满意答案
您无法更改已存在的形状阵列的形状。
要做这样的事情,你可以将位置变量绑定到一个新数组。
my @array[5]; @array := my @[7];我不认为在规范测试中有一个关于切换声明的形状数组的测试(我认为不应该存在)
一种可能更安全的方法是声明没有形状的数组并立即将其绑定到具有起始形状的数组;
my @array := my @[5]; @array := my @[7];我说这个更安全的原因是编译器可能会进行一些优化,假设它总是具有声明的形状。
@array[*-1] => @array[4] @array[6] => Failure.new(…) # ( or even a compile-time error )You can't change the shape of an already existing shaped array.
To do something like this you can bind the positional variable to a new array.
my @array[5]; @array := my @[7];I don't think there is a test for switching out a declared shape array in the spec tests ( nor do I think there should be )
A probably safer way is to declare the array without a shape and immediately bind it to an array with the starting shape;
my @array := my @[5]; @array := my @[7];The reason I say this is safer, is a compiler might do some optimizations that assume it will always have the declared shape.
@array[*-1] => @array[4] @array[6] => Failure.new(…) # ( or even a compile-time error )
更多推荐
发布评论