lua-users home
lua-l archive

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

>> -- Index Class No Function Calls
>> local proxy = setmetatable({greeting = "hi"}, {__index = function(tab) return
>> (tab, "greeting") end})
>> local test = setmetatable({ }, {__index = proxy})
>> print(test.greeting)
>> -- Lua: hi
>> -- Modified Lua: hi
>I did test this. It is example 3 in my previous post. I don't get the
>results show above, but instead
>-- Lua: hi
>-- Modified Lua: nil
>In the modified Lua, since the index function in proxy is passed the
>test table, how does it obtain the "hi" value for greeting which is in
>the proxy table?

Example 3 is a different case, you try indexing test with unknownkey - the above
case is indexing it with greeting; where greeting exists in (what I use as a)
class table.

If you do mean the above example, I don't quite understand how it could not work.

Starting from the bottom, working up, in psuedocode the events should be:

index(test, "greeting")
(is nil)
index(getmetatable(test), "__index")
(is a table, proxy)
index(proxy, "greeting")
(is not nil, return "hi")

Eg, no function calls are made and should be the same for both generic lua and
with the modification.

I think the above description is probably clearer then I've given before, so
might continue the example to show the modification proposed:

index(test, "unknownkey")
(is nil)
index(getmetatable(test), "__index")
(is a table, proxy)
index(proxy, "unknown key")
(is nil)
index(getmetatable(proxy), "__index)
(is a function)
Normal Lua: call(function, proxy, "unknownkey")
Modified Lua: call(function, test, "unknownkey")

Which in my opinion is more useful, as it allows the implementation of
getter/setter functions for an object orientated approach to lua without any
closures being created, and with normal methods being resolved without any
function calls. Just imo. /shrugs

- Alex