develooper Front page | perl.perl5.porters | Postings from July 2016

Re: [perl #128678] Wish: 'hard assignment' operator

Thread Previous | Thread Next
From:
Aristotle Pagaltzis
Date:
July 20, 2016 18:19
Subject:
Re: [perl #128678] Wish: 'hard assignment' operator
Message ID:
20160720181925.GA83937@plasmasturm.org
* Ed Avis <perlbug-followup@perl.org> [2016-07-20 15:48]:
> I don't know about you, but I find my code full of checks like this:
>
>     foreach my $i (@items) {
>         die "seen $_ twice" if defined $score{$i};
>         $score{$i} = get_score($i);
>     }
>
> (Here 'exists' could be more appropriate than 'defined' if the score
> itself can be undef, but for the scope of this discussion I am
> considering definedness.)
>
> Or perhaps they can be written in a more 'topical' way as
>
>     foreach my $i (@items) {
>         for ($score{$i}) {
>             die if defined;
>             $_ = get_score($i);
>         }
>     }

You don’t need a new language feature.

    sub ensure_undef ($) : lvalue { defined $_[0] ? croak 'Unexpected defined value' : $_[0] }

    foreach my $i (@items) {
        ensure_undef $score{$i} = get_score($i);
    }

Regards,
-- 
Aristotle Pagaltzis // <http://plasmasturm.org/>

Thread Previous | Thread Next


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