On Mon, 2002-07-22 at 04:36, [EMAIL PROTECTED] wrote:
Now, I ask for PMC programmers to take care implementing this! Notice
that, for example in arrays, arrays with the same length but different
elements should return different hash codes (or try). But for the same
elements MUST return the same hash code.
Um... not necessarily. Bordering on the 'not at all'. Perl 6 will
apparently allow one to have things other than strings as keys to
hashes. If I have:
$a = [ 1, 2, 3 ];
$b = [ 1, 2, 3 ];
%foo{$a} = 'A';
%foo{$b} = 'B';
Then I want C (%foo{$a} == 'A') %foo{$b} == 'B' to be true.
That's correct.
Hmm... actually, that doesn't require a hashing algorithm which
returns distinct values for $a and $b does it...
Also correct.
Actually there's a whole can of worms here. Assuming I've just run
the above code. I want to be able to do C $b[0] = 3 and still be
able to lookup %foo{$b}, which implies that Chash should be based
on some invariant of the PMC that's independent of its content.
And that is also correct. Larry has discussed the idea that the string
form of the key (which would be used for hashing) would still be the old
reference notation ala Perl5, so that would not change. The thing that
does change is that Perl6 stores the *actual key* and hashes it when
required (presumably with some sort of internal caching), where Perl5
stores Cscalar(key) and does not maintain a copy of or reference to
the original.
But then sometimes you'd *want* hashing to be based on the
content. Hmm. Assuming $b hasn't been modified, how about:
That's fine. When you want that you could declare it as a property, and
that property could be defined in a module. There are many (infinite?)
ways that you might want to hash complex data structures, and all of the
really useful ones should be supported. But, there's no reason for that
support to be in the core language.
So, for example:
my %w is Hashed::ByLength;
my %x is Hashed::ByReference;
my %y;
%y{%w} = 'A';
%y{%x} = 'B';
my %z is Hasher::ByContent;
%z{%w} = 'C';
%z{%x} = 'D';
In this example, I posit two types of property. One describes how an
object is hashed when stored (e.g. Hashed::ByLength and
Hashed::ByReference). The other describes how a hash hashes its keys
(which would override the former because it never calls, e.g.
C%w.hashkey or whatever the method is that Perl will call to hash an
object).
This is all off the top of my head, and you could define modules that
would work a different way, but my point is that I think you can get
everything you want in Perl6 as planned (assuming that user code will be
able to override hashing, which seems to have been in the plan).