4

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

1 Answers1

5

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 )
Brad Gilbert
  • 33,846
  • 11
  • 78
  • 129
  • I am still new to perl6 coming from a Java background i can do int x[]; and then later in another line I can do x= new int[5]; I was wondering if the same was possible here. – AphroditeVenus Jan 19 '17 at 22:38
  • @AphroditeVenus Why don't you just use the normal arrays? In Java (I assume) you have to do that because of limitations in the design, Perl (5&6) try to remove as many limitations as possible. The reason Perl 6 even exists is because there were limitations in Perl 5 that couldn't be worked around easily. – Brad Gilbert Jan 20 '17 at 00:53
  • wouldn't forcing the size of the array cause faster compilation? Also I need its constraint checks – AphroditeVenus Jan 20 '17 at 01:59
  • 1
    @AphroditeVenus Forcing the size slows down compilation, and possibly even slows down the runtime. I think it even has to create a new class just for arrays of a given shape. The native shaped arrays can sometimes be faster, but they are generally only used in code that needs to be very heavily optimized. – Brad Gilbert Jan 20 '17 at 04:22
  • 1
    Eventually, shaped native arrays *will* become faster than ushaped arrays. At the moment however, there's still a lot of boxing to HLL / unboxing from HLL constructs going on that need to be optimized away. A great part of the design of Perl 6 is to allow optimizers to have the necessary introspection to be able to do this. It's just that no one has had the tuits to write those optimizers (although quite a lot of optimizing is going all already: compare your code running with and without --optimize=off :-). – Elizabeth Mattijsen Jan 20 '17 at 11:10
  • 1
    putting an Array that has a shape into a variable that doesn't have a shape as part of its declaration will work with binding just like @BradGilbert suggests in this answer. It will then enforce bounds checks for you, but the variable will still allow you to bind a new array with a different shape later on. This will not give you trouble with future optimizations, either. – timotimo Jan 20 '17 at 20:10
  • @BradGilbert so enforcing the type generally slows down compilation speeds even for scalar variables? – AphroditeVenus Jan 20 '17 at 21:33
  • 1
    @AphroditeVenus It really depends, there is still a lot of optimization work to be done. Currently if you add type annotations it could slow it down because it has to check the types. In some cases it speeds it up though. I wouldn't worry about it though, just add ypes where it makes sense. – Brad Gilbert Jan 20 '17 at 22:36