Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re: RFC: "assignary" operator ?= :

by shmem (Chancellor)
on Dec 07, 2019 at 22:25 UTC ( #11109823=note: print w/replies, xml ) Need Help??


in reply to RFC: "assignary" operator ?= :

I like it. Very much. Looks to me like a very concise and logical addition to operators modifying the LHS, like *= /= ||= and such. And it is readable, fluently!

$var ?= "something" : "something else";

"If $var is set, assign to it 'something', else 'something else'"

"Elegant as f*ck", as a cow-orker coined a python expression.

I'd really like to have that operator added. If I find the time (and brainz) to whip up a proposal with patches, I'll do. Hopefully.

Thanks!

perl -le'print map{pack c,($-++?1:13)+ord}split//,ESEL'

Replies are listed 'Best First'.
Re^2: RFC: "assignary" operator ?= :
by LanX (Archbishop) on Dec 08, 2019 at 16:35 UTC
    What's the use case which justifies extending Perl's codebase for such an exotic feature?

    I can hardly imagine a reason to map non-Boolean values to a previously Boolean $var without confusing any maintainer.

    Using a new variable is always better then.

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery FootballPerl is like chess, only without the dice

      What's the use case which justifies extending Perl's codebase for such an exotic feature?

      Concise expressiveness. The ternary ? : is a shortcut for a more convoluted simple if/else

      my $var; if($cond) { $var = 'this'; } else { $var = 'that'; }

      to be written as

      my $var = $cond ? 'this' : 'that';

      which is one line compared to 5, 6 or 7, depending on indentation style.

      We have more of such subtle operators, eg. ||= vs. //=, where the first checks the LHS truthiness, and the second checks the LHS definedness.

      Writing

      $var = $var ? 'this' : 'that';

      just looks and feels as silly as

      $value = $value * 5; $next = $next + 1;

      instead of

      $value *= 5; $next++;

      For the same reasons, I like the compound operator x!! so much, because it lets me set up a parameter list based on truthiness of variables:

      $result = build_shed( logs => 24, (screws => 120) x!! @screwdrivers, (nails => 360) x!! @hammers, );

      Otherwise, I'd had to say:

      my %materials = (logs => 24); $materials{screws} = 120 if @screwdrivers; $materiasl{nails} = 360 if @hammers; $result = build_shed(%materials);

      While the second variant is one line less, I regard the first variant as much more readable, and I don't have to introduce a temporary hash just for the sake of building function arguments. Note that in the second variant, the hash name is misspelt as materiasl at the 'nails' case, small bug caught by strict, but annoying. So, instead of

      $var = $var ? 'this' : 'that';

      I'd rather like to see

      $var ?= 'this' : 'that';

      because it binds the condition to the LHS in the same way as ||= and //= do.

      I can hardly imagine a reason to map non-Boolean values to a previously Boolean $var without confusing any maintainer.

      Who said that $var was boolean? It could hold any value, and this value is checked for truthiness.

      Anyways - de gustibus non est disputandem.

      perl -le'print map{pack c,($-++?1:13)+ord}split//,ESEL'
        > I like the compound operator x!!

        It's a bit different than the if/else or ternary it's used to replace, though. Unlike them, the x!! evaluates the left hand side even if the condition is zero. Cf.

        my $t; sub tuple { $t++ => 12 } my $bool = 0; my %h = (tuple()) x !! $bool;
        versus
        ... my %h = $bool ? (tuple()) : ();

        Now %h is the same, but $t is different.

        map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]
        The semantics are already perfectly understood, but you haven't shown us a use case.

        Edit: When and why does this ever happen???

        > $var = $var ? 'this' : 'that';

        Cheers Rolf
        (addicted to the Perl Programming Language :)
        Wikisyntax for the Monastery FootballPerl is like chess, only without the dice

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://11109823]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (5)
As of 2020-06-07 10:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you really want to know if there is extraterrestrial life?



    Results (42 votes). Check out past polls.

    Notices?