lua-users home
lua-l archive

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

On Sunday, May 3, 2015, Nagaev Boris <> wrote:
On Sun, May 3, 2015 at 4:39 PM, Dirk Laurie <> wrote:
>> PS 2. Why no individual metatables for light userdata?
> They would no longer be light. A light userdata is like a number:
> it's just a pointer.
Light userdata's advantage over normal userdata is being a number in
context of table's key. In other words two light userdata pointing the
same memory (sharing the same address) are *one* key in a table.
Normal userdata with values of pointers are two keys in a table. The
only workaround I know is using table, mapping light userdata to
normal userdata with same value.


Best regards,
Boris Nagaev

This thread should be catalogued under the general "metatable" heading and not so much just strings. 

But, of course Lua has everything that you need and I don't think you'd disagree with that, even as you may want something more sophisticated. 

I think that these topics boil down to sub-classing / inheritance. Like many features it is not one, but two orders of mechanisms away:

1: choose (more likely recreate from scratch) a class library. 
2: subclass strings
3: work around edge cases where that won't work because not everything works with metatables when on of the "objects" doesn't have one or when you've "inhereted" from the base type, without embuing that base type with extra sauce to deal with your class model

    url_object .. "5" --okay
    string_of_url .. url_object --probably not okay

    string_of_url == url_object --sometimes equal, but maybe not? Depends on the fanciness of your __eq method, probably. 

So, here again, everything that you want can be had, by simply adding metatables to the base types. But, if you think like I do, you don't do that because you don't want to slow *everything* down (even though you / I haven't benchmarked that) and you / I dream that our code will some day become a module that everyone will want to use, just like in Ruby or JS. 

It's a conundrum that I think everyone contemplates and re-contemplates (and re-...). It's why I say that the best thing for Lua is MoonScript. 

Specifically: if you are embedding Lua and want to do these things, then modify the metatables, benchmark it and let us know how it went (seriously. I'm not just being negative about your ideas here). If you want to use Lua like Perl or Ruby, then try that in MoonScript and see if you can't get it done.

Does that sound reasonable?