Brian,

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:
> (1, (2,3), 4).flat;
(1 2 3 4)

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

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!

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?

Many thanks for your help!

Cheers,
Jules.

On 03/01/2016 02:23, Brian S. Julin via RT wrote:
This is actually a fallout of the interaction of two intentional design 
features:

1) Arrays itemize (wrap in Scalar) everything assigned to them
and
2) Itemization (Scalars) protect against flattenning

Which makes it reasonable to ask, why does Array have a ".flat" method if it
cannot do anything?  However, if you force the array not to itemize using a
direct bind (which bypasses the assignment itemization, and is generally
discouraged) you should be able to observe flattening in Arrays.

Indeed this actually does seem to be broken,

$ perl6 -e '
my @a = (1,23,4);
@a[1] := (2,3);
@a[1].VAR.WHAT.say;
@a.flat.say;'
(List)
(1 (2 3) 4)

Now, if running an Array through an iterator shold return the Scalars, rather
than the values, this will remain a design wart even after fixing the above.
There have been several users on IRC lately running into this in one form or
another, many because some packages are electing to return nested arrays rather
than nested lists even when their return values are not useful as mutables.

If an array should de-itemize during iteration, then Array.flat would behave
intuitively, but that could have lots of other impacts.  If Array.flat were
special-cased, that would sweep the wart slightly under a rug until someone
who wants to selectively preserve structure finds that inconsistent.




Jules

--
ju...@jules.uk
Twitter: @JulesFM

'Ever since the dawn of civilization, people have craved for an
 understanding of the underlying order of the world: why it is as
 it is, and why it exists at all. But even if we do find a complete
 theory of everything, it is just a set of rules and equations. What
 is it that breathes fire into the equations, and makes a universe
 for them to describe?' - Stephen Hawking


--
This message has been scanned for viruses and
dangerous content by MailScanner, and is
believed to be clean.

Reply via email to