lua-users home
lua-l archive

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




On 18/07/16 06:26 AM, steve donovan wrote:
On Mon, Jul 18, 2016 at 9:16 AM, Rain Gloom <raingloom42@gmail.com> wrote:
If I may play devil's advocate: what about simply using a special data
structure for such special cases? Ie.: just implement it as a feature of a
usual table with an __index and __newindex metamethod?
Not such a devilish idea; why not indeed? That's the method which has
worked fine for us in the past

As Philipp Janda said earlier:

You want `t[bigint(3)] = x` to be equivalent to `t[3] = x`, but `bigint(3)+bigint(3)` should use custom big integer addition, *not* the built-in Lua integer addition. For `bigint("2^128")` you'll still use an interned userdata so that `t[bigint("2^128")] = 1; print(t[bigint("2^128")])` works as expected (`__key()` would just return the userdata itself in this case).

Another example would be a UTF8String where `#` returns the number of characters not bytes, but which behaves like a regular string when used as a key in a table (and preferably when compared to a regular string via `==`, but that's a different story).

--
Disclaimer: these emails may be made public at any given time, with or without reason. If you don't agree with this, DO NOT REPLY.