lua-users home
lua-l archive

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


(also, if you really don't like to type 'self', you could just add
"_ENV=self" at the top of your functions, no need to break Lua)

As I keep on exploring 5.2, I'm realizing that there are now really quite a number of ways to get an implicit self, using various sorts of _ENV tricks.  For example, if you really want a C++-style member function call syntax, you can get it by just contriving to define all your member functions in a closure where _ENV=self.  That leads to somewhat bloated class instances -- as you end up needing to duplicate each member function for each instance -- but, it's certainly a reasonable approach in some cases.

My parser hack for member function calls imposes less memory overhead, but, it also leads to uglier, more obscure code.  An interesting middle ground is to set classes up with an __index metamethod that wraps any calls to member functions in small closures that insert self into the start of their argument list.  This leads to cleaner code than the parser hack, while still being relatively memory efficient, compared to the heavyweight class strategy.

It really is a wonderfully flexible language.

-Sven