lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On Tue, Jul 20, 2010 at 11:42, Alexander Gladysh <agladysh@gmail.com> wrote:
> On Tue, Jul 20, 2010 at 11:38, Valerio Schiavoni
> <valerio.schiavoni@gmail.com> wrote:

>> if I want to use tables as keys, is it possible to compare the new key
>> with existing keys by value (and not by reference) ?
>
>> In general, what is the approach in using complex datatypes as keys in tables?
>
> <...>

>> I think in java this is the common scenario where a custom equals()
>> method is implemented to avoid duplicates in a container.

>> Do I need to implement my own table implementation perhaps?

> You need to do your comparison manually.

<...>

Err. Sorry. I misunderstood your quesrion. You asked for custom hash
function for table keys, not for table comparison.

As Javier suggested, using string representation of tables as keys is
probably the only solution to your problem. Note that you may wrap it
into a metatable nicely to get a better syntax. Even auto-updates of
the keys on table change are probably doable. Not much can be done for
the speed penalties though.

So, it may be that what you want to do is not a "Lua way" thing, and
you should reconsider your architecture. :-)

Alexander.

P.S. As twitter users could see, there was дождь (rain) in Moscow
today. As you can see from my posts today, it did not improve
situation much. Still the same mind-numbing heat. Now with extra
humidity.