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

Re: The ,= operator

Thread Previous
From:
William Michels via perl6-users
Date:
November 27, 2020 17:39
Subject:
Re: The ,= operator
Message ID:
CAA99HCyzDVJer5knAzF6H4dPhAaN00a4y+46DfCbLWGqvGF=hg@mail.gmail.com
Hi Joe,

I can reproduce your results on Rakudo_2020.10, but I'm afraid I don't
have much more to say about the ",=" operator since I'm unfamiliar
with it.

Do the "docs" page(s) make more sense changing the phrase
"class-dependent" behavior to "hash-dependent" behavior?

Best, Bill.


On Thu, Nov 26, 2020 at 2:29 PM Joseph Brenner <doomvox@gmail.com> wrote:
>
> I was going through the operator list in the documentation the
> other day, and I noticed this one:
>
>   postfix ,=
>
>   Creates an object that concatenates, in a class-dependent way,
>   the contents of the variable on the left hand side and the
>   expression on the right hand side:
>
>      my %a = :11a, :22b;
>      %a ,= :33x;
>      say %a # OUTPUT: «{a => 11, b => 22, x => 33}␤»
>
> I was playing around with this a bit and now I'm wondering what
> it's talking about with that "class-dependent way".
>
> To start with, I've got a small issue with that example:
> that particular pair-input syntax is my least favorite.
> Flipping around the order of key and value when the value is a numeric...?
>
> And it isn't needed to demo the operator, any pair input syntax works.
> I might argue that examples should favor the form that "say" is
> going to spit back to you.  This would be clearer at a glance:
>
>    my %b = a => 11, b => 22;
>    %b ,= x => 33;
>    say %b; # OUTPUT: «{a => 11, b => 22, x => 33}␤»
>
> But where it gets interesting is trying a ,= on something besides
> a hash.  Let's try arrays, where I would expect this to push a
> value onto the array:
>
>   my @r = 'a', 'b', 'c';
>   say @r; # [a b c]
>   @r ,= 'd';
>   say @r; # (\Array_53270704 = [Array_53270704 d])
>   dd @r;  # Array @r = ((my @Array_37059224) = [@Array_37059224, "d"])
>
> It's difficult for me to even see what that is or how I would use it:
>
>   say "keys: ", @r.keys;      # keys: (0 1)
>   say @r[0];  # (\Array_44472736 = [Array_44472736 d])
>   say @r[1];  # 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.
>
> Whatever this "class-dependent" behavior is, I would suggest
> it's a "fail".  Could this be a bug?

Thread Previous


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About