Hi,

I would like to give a perspective a bit different to what yary provided.

Your example here can be golfed down to:

my $a = [1,2,3];
my @v = $a;
say @v; # [[1,2,3],]

The reason for this behavior is $a being a scalar container. Correspondingly, 
when you  assign it to @v the assignment op see a single element, not a 
positional. So, it behaves correspondingly. Apparently:

my @v = [1,2,3];
say @v; 

Do what's expected. The reason is [1,2,3] on RHS is a positional. It's a 
non-containerized value. So, if we put one and one together we come to the 
following assignment: 

my @v = $a<>;
say @v; # [1,2,3]

Because by stripping of the container with <> operator we get a positional on 
RHS. Now, if we unroll it all back your case, the only thing we should remember 
is that arrays containerize any value assigned into them. I.e.:

constant foo = 4;
my @v;
@v[0] = foo; 
say 4.VAR.^name; # Int
say @v[0].VAR.^name; # Scalar

Therefore, when you do @first = @both[0] – it is equivalent to @v = $a in the 
first example.

We have a documentation section on this: 
https://docs.raku.org/language/list#Itemization

Best regards,
Vadim Belman

> On 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