Let's dig in a little

 my @one = 1,2,3;
 my @two = 4,5,6;
 my @both = @one,@two;

at this point @both is an array containing two arrays

> dd @both
Array @both = [[1, 2, 3], [4, 5, 6]]

Showing that assigning into an array variable gives an array, each element
of which can itself be an array.

What is @both[0] - ah it is an array

> dd @both[0]
Array @both = $[1, 2, 3]

So I expect assigning it intoan array to result in an array containing a
single array

> my @first = @both[0];
[[1 2 3]]
> dd @first
Array @first = [[1, 2, 3],]


And if I want it to match the original @one array, then I should retrieve
from index 0

> dd @first[0]
Array @first = $[1, 2, 3]
> dd @first[0] === @one
Bool::True

When you use binding := Raku no longer puts items into a new container, it
makes the thing on the left an alias for the thing on the right. No new
container means no outer array holding things assigned to it.

That all matches my intuition... does it help you?

-y


On Tue, Jan 19, 2021 at 1:18 PM Brian Duggan <bdug...@matatu.org> wrote:
>
> Hi Folks,
>
> I ran into this situation today, which seems counterintuitive:
>
>  my @one = 1,2,3;
>  my @two = 4,5,6;
>  my @both = @one,@two;
>  my @first = @both[0];
>  say @one.raku;
>  say @first.raku;
>
> output:
>
>         [1, 2, 3]
>         [[1, 2, 3],]
>
> I was expecting @first and @one to be the same.
> I discovered that I could instead write either of these --
>
>   my (@first) = @both[0];
>   my @first := @both[0];
>
> or I could change the @both assignment to be
>
>         my @both := @one, @two;
>
> ..but I wonder if there's an idiomatic approach -- or
> way of thinking about this -- that makes this flow more
> intuitive.
>
> thanks
> Brian

Reply via email to