lua-users home
lua-l archive

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

> Sorry to be so annoying and rejecting your suggestions, but the __call
> metamethod is already used in theses objects. (and this also break
> compatibility with existing code)
> I've just doesn't realize it need so much changes to alow __index to
> return multiple values. After looking at the lua code I've take away the
> idea of a patch, it imply too much change, too much testing and overhead
> just to satisfy my eyes when looking at some small parts of my code ;-)
> I will stuck with a new method in my object, an __index shortcut and
> try to forgot this dream...
> Tom

This is probably as close as you will get:

> t = { }
> r = { }
> m = { }
> m.__index = function()
>>   return function()
>>     return unpack(r)
>>   end;
>> end;
> r[1] = "hi"
> r[2] = "bye"
> r.hi = "bye"
> setmetatable(t, m)
> return t.hi()
hi      bye

A problem with what I put above is that r must hold a list with consecutive elements starting at [1]. This isn't necessarily true, you could have a for loop that iterates over pairs(r), but then you would need a table to wrap all the elements in (you didn't want extraneous tables made).

Calling t.hi() is really painful IMO. If t["hi"] actually exists then you will get an error (if it is not a function). If you suspect t.hi does not exist (you set your variables up expecting a list returned), then you should probably use something like:

x, y = t.hi or unpack(r); --t has no __index metamethod

There are no extra tables made, just some function overhead.

Your problem is that __index is meant to find >an< (one) index of the table. It would be senseless for an index of a table to be a list.

I also think whatever time you may save by having GC happen less often will be lost by all this metamethod nonsense to make the code look pretty (sorry if that sounds harsh) : P


-Patrick Donnelly

"One of the lessons of history is that nothing is often a good thing to do and always a clever thing to say."

-Will Durant