> On 03 Jan 2016, at 10:44, Jules Field <ju...@jules.uk> wrote:
> The snag is this:
> > my @a = (1, (2,3), 4);
> [1 (2 3) 4]
> 
> I asked for a List, but got given an Array. So I can do this:

If like that, you really have asked for an Array (@a) to be assigned with the 
values of a List (1, (2,3), 4).  If you want @a to be a List, you need to 
*bind* it to the List.

$ 6 'my @a = (1, (2,3), 4); say @a.WHAT'
(Array)
$ 6 'my @a := (1, (2,3), 4); say @a.WHAT'
(List)

> > (1, (2,3), 4).flat;
> (1 2 3 4)
> 
> but not this:
> > @a.flat;
> (1 (2 3) 4)

.flat returns a Seq

$ 6 'my @a := (1, (2,3), 4); say @a.flat.WHAT'
(Seq)


> And now @a.flat is a list. But if I assign the result to @b then this happens:
> > my @b = @a.flat;
> [1 (2 3) 4]
> 
> Now it's turned into an Array again!

Again, you need to bind if you want @b to be a List.  Because you can’t bind a 
Seq, it needs to be coerced to a List:

$ 6 'my @a = (1, (2,3), 4); my @b := @a.flat.list; say @b.WHAT'
(List)


> How can it be right that if I split 1 line of code into 2 by creating an 
> intermediary variable, the code behaves totally differently?
> If it's intentional, then what am I doing wrong in my trivial example above?

Because the @ sigil forces an Array by default.  You don’t have to use an @ 
sigil though:

$ 6 'my $a = (1, (2,3), 4); my $b = $a.flat.list; say $a.WHAT; say $b.WHAT'
(List)
(List)

> Many thanks for your help!

Hope this helps…



Liz

Reply via email to