I recently tried my Array @a = 'a'..'z';
and my Array @a = @('a'..'z');
.
Both will produce the following error:
Type check failed in assignment to @a; expected Array but got Str ("a")
in block <unit> at <unknown file> line 1
However initializing without the type works and seems to ultimately produce an Array:
> my @a = 'a'..'z';
> @a.^name
Array
Why is this the case?
To initialize or instantiate an array as we declare it, meaning we assign values as when we create the array, we can use the following shorthand syntax: int[] myArray = {13, 14, 15}; Or, you could generate a stream of values and assign it back to the array: int[] intArray = IntStream.
We can declare and initialize arrays in Java by using a new operator with an array initializer. Here's the syntax: Type[] arr = new Type[] { comma separated values }; For example, the following code creates a primitive integer array of size 5 using a new operator and array initializer.
To create an array, define the data type (like int ) and specify the name of the array followed by square brackets []. To insert values to it, use a comma-separated list, inside curly braces: int myNumbers[] = {25, 50, 75, 100}; We have now created a variable that holds an array of four integers.
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.
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:
It looks like a zero digit (0
) with an ð
(Mathematical Italic Small A) inside it -- and a @foo
variable is by default a 0
indexed ðððððĶ
(or @ððððĶ
).
It sounds like the word "at". An array has elements at indices.
Set the type of the element in the array:
my Str @a = 'a'..'z';
say @a; #[a b c d e f g
To see, what type it is, you can use .WHAT
my Str @a = 'a'..'z';
say @a.WHAT #(Array[Str])
To test if it is an array, you can smartmatch
my Str @a = 'a'..'z';
say 'is array' if @a ~~ Array; #is array
say 'is str array' if @a ~~ Array[Str]; #is str array
say 'is str array' if @a ~~ Array[Int]; #
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With