Nigel Sandever writes:
> On Sun, 27 Feb 2005 15:36:42 -0700, [EMAIL PROTECTED] (Luke Palmer) wrote:
> > As far as getting 2, 2.0, and "2" to hash to the same object, well, we
> > know they're 'equal', so we just need to know how to hash them the same
> > way.  In fact, I don't believe 2.0 can be represented as a Num.  At
> > least in Perl 5, it translates itself back to an int.  So we can just
> > stringify and hash for the scalar types.
> 
> My thought is that if c<my %hash is shape(Any)> uses the stringyfied values 
> of 
> the keys, then it is no different to C<my %hash>,

Indeed, but I meant just for our non-reference scalar types, such as
Num, Int, and Str.

> I think it would be useful for shape(Any) be different to an ordinary
> hash, and hashing the binary representation of the key, so that 
> 
> (Int)2, (Num)2, (String)2, (uint)2 (uint4)2 etc.
> 
> would be a useful way of collating things according to their "type"
> rather than their value....?

That may indeed be a useful kind of map to have, but it's hardly what
people expect when they declare a hash keyed by Any.  The reason I want
to identify 2 and "2" is because they are identical everywhere else in
the language.

Also, if you hash on the binary representation, what's to say that uint
hashes differently from uint4.  And even if we do guarantee that they
hash differently, there will be collisions.  And then uint(2) equal
uint4(2) (for any reasonable implementation of equal), and they are the
same, but only for collision values.

A better way to make a type-collated hash would be to:

    class TypeHash is Hash[shape => [Class; Any]] {
        method postcircumfix:<{ }> (Any $arg) {
            .SUPER::{$arg.type; $arg}
        }
    }

And if you really think that it's going to be that common, then you can
write a module that implements that.  It would be about five lines long.

Luke

Reply via email to