develooper Front page | perl.perl6.users | Postings from November 2020

Re: The ,= operator

Thread Previous | Thread Next
Joseph Brenner
November 28, 2020 01:49
Re: The ,= operator
Message ID:
First off, much thanks to Ralph Mellor for his detailed explanations.

Ralph Mellor<>  wrote:

> @r ,= 'd';
> The above expands to:
> @r = @r , 'd';

Okay, that makes sense.  So the circular reference I thought I
was seeing is really there, and it's working as designed.

There isn't anything very useful in this behavior though, is there?

Also (the immediate point, really) this isn't described very well
in the documentation.

Just to be thoroughly clear, since with a hash this works:

    my %all_at_once = a => 1, b => 2, c => 3;

    my %in_stages = a => 1;
    %in_stages ,= b => 2;
    %in_stages ,= c => 3;

(Now %in_stages is the same as %all_at_once.)

My first guess was that for arrays, this would work:

    my @all_at_once = 'a', 'b', 'c';

    my @in_stages = 'a';
    @in_stages ,= 'b';
    @in_stages ,= 'c';

But here, while @all_at_once is just: [a b c] the array
@in_stages has a peculiar structure containing circular references.

> >   say @r; # (\Array_53270704 = [Array_53270704 d])
> >   dd @r;  # Array @r = ((my @Array_37059224) = [@Array_37059224, "d"])

> I get both the `gist` (`say`) and `dd` are weird.

> But that's because the value is both infinite and recursively so.

> What else can they to do?

I'm not sure there is much else they could do.  Flagging the
circularity in some way?

   say @r; # ( [<CIRCULAR REFERENCE> d])

> > So it *does* push a 'd', but it first mangles the original
> > contents of the array.  It looks like @r[0] now has some sort
> > of circular pointer back to @r.

> It's doing exactly what your code told it to do, and the display
> is doing perhaps the most useful thing that can be done, as
> affirmed by the fact you've correctly guessed the right way
> about the circular pointer, but have wrongly guessed that that
> is a resulting of it "mangling"..

Well, actually, I noticed the circular reference by playing with
.keys and explicitly dereferencing with [0] and [1].

And not to quibble too much, but if you didn't want this to do
that to the array, then it *is* "mangling" it.  I get that it's
working as designed, but I think that means you're never going to
want to do this.  It could be it should even be warning or
erroring out.

> > Whatever this "class-dependent" behavior is, I would suggest
> > it's a "fail".  Could this be a bug?

> It isn't really anything to do with "class dependent" behaviour.

In which case, the documentation could use some improvement.
(Actually, saying that some behavior in Raku is class dependent is
kind of redundant... it's all class dependent in one way or another.)

> Perhaps more importantly, what improvement do you propose?

Well, I just mentioned a few.  I always want to see more examples
in the docs.  What else would you use ,= with besides a hash?

Just as an aside:

It could be my confusion here is another form of something else
that keeps tripping me up.   Merging two hashes together is
pretty simple:

  my %h_all = %h1,  %h2;

But doing the same thing with arrays is going to require slipping
the values out of the containers:

  my @a_all = | @a1,  | @a2;

So the two cases aren't all that parallel.

Thread Previous | Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About