[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: USERDATA getters, setters and methods requirement
- From: Sam Roberts <vieuxtech@...>
- Date: Wed, 5 Jan 2011 12:46:17 -0800
IIRC, what you are trying to do, have one __index that knows only
about attribute names, not methods, and have it be the metatable of
another __index that is a table of methods won't work, because the
wrong self pointer gets sent along. You need one __index that knows it
can be called with the name of a method (foo:dothis()) or name of a
"property" (foo.x = 3), and does the right thing for either case.
I've done something like this before, but a few years ago. I think
what I did is to put the "methods" in a table in the registry, and I
implemented __index(name) to look like (in c):
if name == "x" then return theobject->x
elseif name == "y" then return theobject->y
else
methods = ... get it from the registry
return methods[name]
end
__newindex() was similar.
These examples might help:
http://lua-users.org/wiki/ObjectProperties
http://lua-users.org/wiki/BindingWithMembersAndMethods