TL;DR I provide a relatively simple answer in Why is this the case? However, that explanation may be inadequate1 so I review some alternatives in Declare and initialize a typed array from a range.
Why is this the case?
my @a;
declares a new Array
(initialized to be empty) and "binds" it to the symbol @a
. Thus my @a; say @a.^name
returns Array
. There is no need to use the word Array
in a declaration or initialization of an array -- the @
is enough.2
my @a = 'a'..'z'
attempts to copy each value in the range 'a'
thru 'z'
, one at a time, into @a[0]
, @a[1]
, etc. The new array bound to @a
has a type constraint for each of its elements (explained in the next section); it will be checked for each value (and will succeed).
my Array @a
declares an Array
with an Array
type constraint on its elements (so it's an array of arrays). my Array @a; say @a.^name
returns Array[Array]
to indicate this. my Array @a = 'a'..'z';
fails when copying the first value ("a") because it is a Str
value not an Array
.
Declare and initialize a typed array from a range
my @a = 'a'..'z';
The my @a
part of this statement declares a variable that's bound to (refers to) a new array of type Array
. Because no element type constraint was specified, the new array's elements are constrained to be consistent with Mu
, the Most unassuming type in P6. In other words it's an empty array ready to contain whatever values you want to put in it. (One could say that say @a.^name
displays Array
rather than Array[Mu]
because the [Mu]
is considered Most uninteresting.)
... = 'a'..'z'
initializes the new array. The initialization has no impact on the array's already established type constraints. It just delivers copies of the strings 'a'
, 'b'
etc. into the array (which auto-expands to receive them into @a[0]
, @a[1]
etc.).
I recommend devs avoid adding explicit type constraints on variables and explicit coercions of values unless they're confident they're desirable. (cf my parenthetical remarks at the end of an earlier SO answer.) That said, you can choose to do so:
my Str @a = 'a'..'z'; # `Array` elements constrained to `Str`
my Str @a = (0..99)>>.Str; # Coerce value to match constraint
Alternatively, P6 supports explicit binding, rather than assignment, of a value or list of values. The most common way to do this is to use :=
instead of =
:
my @a := 'a'..'z'; say @a.WHAT; say @a[25]; # (Range)z
Note how the explicit binding of @a
means @a
has not been bound to a new Array
but instead to the Range
value. And because a Range
can behave as a Positional
, positional subscripting still works.
The following statements would imo be grossly redundant explicit typing but would both work and yield exactly the same outcome as each other, though the first one would be faster:
my Str @a := Array[Str].new('a'..'z');
my Str @a = Array[Str].new('a'..'z');
There's more to discuss about this topic but perhaps the foregoing provides enough for this question/answer. If not, please ask further questions in comments under your original question and/or below.
Footnotes
1 An earlier version of this answer began with:
my Array @a ...
# My array of thoughts raised by this declaration
# and your questing "why?" in this SO question
# began with wry thoughts about complicated answers
# about reasons your array is awry and pedances
(I made up the word "pedances" to mean something that appears to be pedantic but flows nicely when used correctly -- which will happen naturally once you've become familiar with its apparently idiosyncratic but actually helpful nature. More importantly, I needed something that rhymes with "answers".)
2 Here are a couple of mnemonics for the meaning of @
in P6: