[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: Re: hash metamethod?
- From: Sam Roberts <vieuxtech@...>
- Date: Sun, 1 Feb 2009 10:09:10 -0800
On Sun, Feb 1, 2009 at 2:31 AM, Joshua Haberman <firstname.lastname@example.org> wrote:
> Languages like Ruby, Python, etc. allow you to define hash functions that
> apply to your objects. You define a hash function and a comparison
> function, and this lets user-defined types be keys in hash tables based on
> their values.
I'm curious what you would use this for?
I've only seen it used once in ruby, in resolve.rb where DNS names are
a sub-class String to make a case-insensitive (but case-preserving)
string class. It worked fine, but if the language hadn't supported
this it would have been easy to achieve the goals in other ways.
I think you could build in pure lua a data structure class, say Hash,
that uses metamethods to index it's entries based on their __hash().
Would that be an easier approach than patching the runtime, or not
work for what you have in mind?
For userdata, a common approach is to cache them in a weak valued
table, to avoid creating identical userdata.