如何在创建后更改Perl6中数组的大小?(How do I change the size of an array in Perl6 after creation?)

编程入门 行业动态 更新时间:2024-10-26 22:25:28
如何在创建后更改Perl6中数组的大小?(How do I change the size of an array in Perl6 after creation?)

在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 )

                    
                     
          

更多推荐

本文发布于:2023-07-16 09:57:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1126811.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数组   大小   如何在   creation   array

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!