Skip to content

Commit

Permalink
Expands the "fixed size array" section
Browse files Browse the repository at this point in the history
Indexing it also as shaped arrays, expanding a bit the examples, and
adding the changes in 2018.09. Closes #2326
  • Loading branch information
JJ committed Sep 24, 2018
1 parent 9d2706f commit a886210
Showing 1 changed file with 27 additions and 12 deletions.
39 changes: 27 additions & 12 deletions doc/Language/list.pod6
Expand Up @@ -591,33 +591,48 @@ an undefined C<Any>:
@n.default.perl.say; # OUTPUT: «Real␤»
@n[0].say; # OUTPUT: «(Real)␤»
X<|Shaped arrays>
=head2 Fixed size arrays
To limit the dimensions of an C<Array> provide the dimensions separated by C<,>
or C<;> in brackets after the name of the array container. The values of such
an C<Arrays> will default to C<Any>. The shape can be accessed at runtime via
the C<shape> method.
To limit the dimensions of an C<Array>, provide the dimensions separated by C<,>
or C<;> in brackets after the name of the array container in case there is more
than one dimension; these are called I<shaped> arrays too. The values of such a
kind of C<Array> will default to C<Any>. The shape can be accessed at runtime
via the C<shape> method.
my @a[2,2];
say @a.perl;
# OUTPUT: «Array.new(:shape(2, 2), [Any, Any], [Any, Any])␤»
say @a.shape;
# OUTPUT: «(2 2)␤»
say @a.shape; # OUTPUT: «(2 2)␤»
my @just-three[3] = <alpha betta kappa>;
say @just-three.perl;
# OUTPUT: «Array.new(:shape(3,), ["alpha", "betta", "kappa"])␤»
Assignment to a fixed size Array will promote a List of Lists to an Array of
Arrays.
Shape will control the amount of elements that can be assigned by dimension:
=begin code :skip-test<Illustrates exception>
my @just-two[2] = <alpha betta kappa>;
# Will throw exception: «Index 2 for dimension 1 out of range (must be 0..1)»
=end code
Assignment to a fixed size C<Array> will promote a List of Lists to an Array of
Arrays (making then mutable in the process).
my @a[2;2] = (1,2; 3,4);
say @a.Array; # OUTPUT: «[1 2 3 4]␤»
@a[1;1] = 42;
say @a.perl;
# OUTPUT: «Array.new(:shape(2, 2), [1, 2], [3, 42])␤»
As the third statement shows, you can assign directly to an element in a shaped
array too. B<Note>: the second statement works only from version 2018.09.
=head2 Itemization
For most uses, Arrays consist of a number of slots each containing a C<Scalar>
of the correct type. Each such C<Scalar>, in turn, contains a value of that
type. Perl 6 will automatically type-check values and create Scalars to contain
them when Arrays are initialized, assigned to, or constructed.
For most uses, C<Array>s consist of a number of slots each containing a
C<Scalar> of the correct type. Every such C<Scalar>, in turn, contains a value
of that type. Perl 6 will automatically type-check values and create Scalars to
contain them when Arrays are initialized, assigned to, or constructed.
This is actually one of the trickiest parts of Perl 6 list handling to get a
firm understanding of.
Expand Down

0 comments on commit a886210

Please sign in to comment.