lua-users home
lua-l archive

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



Sorry, but in general this cannot be done easily. Accessing the object variables is a problem. Imagine getting part way though a method and it calls something that then tries to use the same method on another object, and you see the problem. You could use setfenv to add magic to your methods, but then each object must have its own copy of each method. I don't think this is a practical approach.

I sympathize with you.  I don't like typing self.this and self.that either.

It sure would be nice if we could type:

function someclass:method(x)
 local this, that in self
 if this and that then this = x + that end
end

On the plus side, Lua has first class functions, is lexically scoped, and dynamically typed, so you don't need to use all that OO stuff like you do in C++ to get around its shortcomings.

You'll get better mileage if you think of Lua as a legible dialect of scheme with infix operators, and a lot fewer parentheses.

- Peter Shook


Bjørn Lindeijer wrote:
Hello,

For some time I have been trying to implement an object oriented structure
in my scripts. I've currently partly succeeded in this using the metatable
method of setting __index to the class an object is an intance of, and
__index of each class to its subclass.

My problem is that I currently have to use a lot of "self" references,
basically for every variable or function I access. My question is whether
this can be worked around, and what would be the best way. I would like the
following rules for variable lookup in a class method:

Look in:
1 - Function parameters
2 - Locally defined variables
3 - Current object variables or methods
4 - Globals

I'd appreciate any help.
Bjørn Lindeijer


_________________________________________________________________
The new MSN 8: smart spam protection and 2 months FREE* http://join.msn.com/?page=features/junkmail