On 4/16/02 11:00 AM, "Mike Lambert" <[EMAIL PROTECTED]> claimed:

> Speaking of which, how do we ensure the immutability of keys being put
> into the hash? I think Perl copied the string, so that:
> 
> $b = "aa";
> $a{$b} = 1;
> chop $b;
> print $a{"aa"};
> 
> still works.
> 
> If we start storing full thingies into the keys of a hash, we either need
> to make deep copies of these, or copy enough to ensure the hashing
> function has all that it needs.
> 
> Say we do:
> $b = new Cat();
> $a{$b} = 1;
> $b->somefunctionthatchangesthehashvalue();
> $a{$b} doesn't find anything, since $b was hashed under it's old identity.

But these aren't really equivalent, are they? In the first, you use a
variable ($b) to create the key, and then a constant ("aa") to reference it.
In the second example, you use the variable to both create and reference the
hash value. So really you should do the same for the Perl 5 example:

$b = "aa";
$a{$b} = 1;
chop $b;
print $a{$b}; # No key found (will it be autovivified, BTW?)

Or you should do the opposite, make the Perl 6 example equivalent.

# "aa" will be used for the hash key, as specified by Cat's
# C<method operator:hash> spec.
$b = new Cat("aa");
$a{$b} = 1;
$b->somefunctionthatchangesthehashvalue();
$a{"aa"}; # Key found.

> I personally liked the stringification of keys. It made things a LOT
> simpler. :)

I suspect that you'll still be able to do this.

David

-- 
David Wheeler                                     AIM: dwTheory
[EMAIL PROTECTED]                                 ICQ: 15726394
http://david.wheeler.net/                      Yahoo!: dew7e
                                               Jabber: [EMAIL PROTECTED]


Reply via email to