lua-users home
lua-l archive

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


2011/4/7 Timothée VERROUIL <dreamothee@hotmail.com>:
> I don't get any error.
> When I write : local toto = myObject.myAttribute ; Lua consider
> myObject.myAttribute as a function

That's __index can be implemented in two ways, and you're using the
wrong one. Your statement is either equivalent to:

local toto = getmetatable(myObject).__index.myAttribute

or to:

local toto = getmetatable(myObject).__index(myObject, "myAttribute")

The first one is used if __index is a table, and the second one if
__index is a function. Since your __index is a table, and the
"myAttribute" field of that table is a function, that's what you get.

To have the expression "local toto = myObject.myAttribute" call a
getter, you need to implement __index as a function (see the manual
for the expected prototype, or ask here).

> and when I print the value of toto, it's nil :(

Actually toto should be a function, not nil. How did you determine
that "myObject.myAttribute" is a function, but "toto" is nil ?