lua-users home
lua-l archive

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


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