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

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

Thread Previous | Thread Next
From:
Kent Fredric
Date:
July 20, 2016 18:00
Subject:
Re: [perl #128678] Wish: 'hard assignment' operator
Message ID:
CAATnKFCg=JLydvmqzV4fQmqJr7bw1bEJg9KsixHGVXXmXpN1_Q@mail.gmail.com
On 21 July 2016 at 01:46, Ed Avis <perlbug-followup@perl.org> wrote:
> The wish is to have a 'hard assignment' operator which will set
> something that's currently undefined, but die if it's already defined.
> Suppose := is used (I am not suggesting that is the best syntax, it
> is just for the sake of example).  Then


This feature is too niche for a top level language syntax.

I'd probably be looking more into a utility for `Hash::Util` that
marks an entire hash, or specific keys in a hash as "write-once",
similar to the existing lock_value function.


  lock_defined( \%score );
  foreach my $i (@items) {
       $score{$i} = get_score($i);
  }

Or at variable level, maybe you want a CPAN Module that adds magic to
variables that makes them write-once ( Akin to the readonly interfaces
)

use Imaginary::WriteOnce;
write_once( $x );
$x = 1 ; # ok
$x = 1 ; # attempt to set defined write-once value "$x" to "1"

But generally the idea of "explode on write" seems very bad.

I can understand "explode on missing", ie:


$x := $y{path};
> not defined $y ? die "Expected path property of $y to be defined, got undef" : $x = $y;


But you wanted the opposite to that, and that disturbs me.

$x := $y{path}
> not defined $x ? die "Variable $x already defined" : $x = $y;

Because I frequently find use for the former of those two examples,
but the latter is rare as hell.


-- 
Kent

KENTNL - https://metacpan.org/author/KENTNL

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