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

Re: [PATCH for discussion] new feature: clamp %hash

Thread Previous | Thread Next
Steve Fink
July 30, 2001 17:01
Re: [PATCH for discussion] new feature: clamp %hash
Message ID:
Nick Ing-Simmons wrote:

> Jeffrey Friedl <> writes:
>>Nick Ing-Simmons <> wrote:
>>|> readonly \%hash;     # clamped
>>I reiterate that I believe that this would be a mistake.
> to which I reiterate that that was pseudo code example of the 
> implementation - not an interface definition.
> I was trying to demonstrate that with "hooks" e.g. by readonl() xsub,
> and minor tweaks to the core adding some "dies" we can implement both.
> I leave it up to list as a whole to discuss what it should look like
> to user.

Language-level idea:

readonly keys %hash;
readonly values %hash;
readonly %hash;

The idea is that you set components of the hash to read-only -- the hash 
keys, or values associated with keys, or everything.

readonly keys %hash -- most obvious meaning is that you cannot add or 
delete from the currently existing keys (the set of keys is immutable). 
If that really is a bad idea, it could just mean that the set of *valid* 
keys is immutable, but you can delete a key and re-add it but not add a 
key that did not exist at the time of setting the hash keys readonly.

readonly values %hash -- which value is assigned to a key cannot change.

readonly %hash -- both of the above

None of the above make the values themselves readonly. That would still be

	readonly($_) foreach values %hash

Reading an invalid key from a hash whose keys are readonly could then 
trigger a warning "value fetched from hash guaranteed to be undefined, 
stupid". Compile-time when possible (constant), run-time otherwise.

Just a thought. Unfortunately not implementable as an xsub. To make it 
xsubbable, make the keys %hash list remember where it came from, or 
allow xsubs to set user-defined context description bits. Both of those 
sound pretty hairy, though the second one is crazy enough to be 
interesting. (And preserves the semantics better.)

Thread Previous | Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About