develooper Front page | perl.perl5.porters | Postings from March 2023

Re: Pre-RFC Deep constants

Thread Previous | Thread Next
From:
demerphq
Date:
March 11, 2023 15:50
Subject:
Re: Pre-RFC Deep constants
Message ID:
CANgJU+V29xgz=-sVYLmcAuc-ow3hL4oj6KU7fO0Wb0wYkxss1w@mail.gmail.com
On Sat, 11 Mar 2023 at 15:20, Matthew Persico <matthew.persico@gmail.com>
wrote:

> How about an MVP that creates just scalar constants?
>

Huh? We have two ways to create "constant" scalars:

use constant var => "foo";
Internals::SvREADONLY((my $var = "x"),1);

Am I missing something?



> That's got to be 90% of what people need, and then we can add
> constant hashes later?
>

We have constant (aka read-only) hashes as well. It is just that they are
*restricted*, which renders them nearly useless for most purposes.

use Hash::Util qw(lock_hash_recurse);
lock_hash_recurse(\%hash);

The "restricted" means that unlike normal hashes they throw an exception
when you try to look something in them that they don't contain. (You can
use lock_keys() to specify keys that are allowed to be in the hash but
which do not yet have values.)  The restricted API is sufficiently useless
that I have never seen them used productively. I have /heard/ people claim
they like them on this list, but I have never seen them used to advantage
in my entire career, on the contrary every time I have seen someone try to
use them they have reversed it out almost immediately afterwards.
Ironically I wrote most of the modern code in Hash::Util to facilitate the
advanced forms of read-only hash. But I did not create restricted hashes
themselves.


> After all, constant hashes are just a bunch of constants for those of us
> with namespace OCD (including myself!).
>

Like I said, it turns out that reasonable people can disagree on what a
constant hash should be. :-) I really wish we didnt have restricted hashes
getting in the way, simple "readonly" hashes would be much more useful than
restricted hashes are.

MMMM, I wonder if there is a way we can extend restricted hashes so they
behave as people would expect a readonly/constant hash to behave without
throwing out restricted hashes entirely. Maybe something like
lock_hash(\%hash, undef); ? Eg, the undef would signal "allow any key".
Hrmmmm...... Watch this space, maybe I can come up with a hack for this.

Yves


-- 
perl -Mre=debug -e "/just|another|perl|hacker/"

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