lua-users home
lua-l archive

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

> I have a little issue with implementing OOP with Lua. The
> partcicular topic here is how to access a member of an
> object ala C++, that is without having to prepend the self.
> string before the actual member name.
I know that issue. I decided to keep the self, after trying
the approach below ;-)
> [...] For this, I have use tag methods that track
> accesses to globals to redirect them to a table (the current
> table), but this requires me to maintain a global variable
> with the current table, which is quite unsatisfactory.
Ok, we need that current-object stack. Right.
> Then I have to call a method at start and at end of any
> method to set/clear the current table to the "self" of the
> new method, and to get rid of that I need to use the call
> hook from the debugger interface, which is *really* dirty.
I chose to modify the methods by a settable method for
classes, like

function classSettable (c, i, v)
   if type(v) == 'function' then
	rawsettable(c, i, function (...)
		local savedObject = currentObject
		currentObject = arg[1]
		call (%v, arg)
		currentObject = savedObject
	rawsettable(c, i, v)

So, provided classX has the tag for which the above
tagmethod is set as settable, each definition
  function classX:methodY (arg1, arg2) ...
gets wrapped with by a closure that takes care of the currentObject.
Looks clean to me, but the overhead may not really be worth the
optical improvement.


 Stephan Herrmann
 Technical University Berlin
 Software Engineering Research Group
 phone: ++49 30 314 73174