lua-users home
lua-l archive

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


On Saturday 01 January 2011 16:10:57 Lucas Zawacki wrote:
> Here's a variation of  your code using closures to create private
> fields https://gist.github.com/762006
> Notice that it pretty much makes the a.foo(a,...) and a:foo(...)
> unecessary but I left it to conform with your example.
> 
> Additional reading on the subject can be done here:
> http://lua-users.org/wiki/ObjectOrientationClosureApproach
> 
> PS.:
> 
> steve code using _newindex is quite interesting! Lua once again
> surprises me (in a good way :)

Hi Lucas,

It turned out that I wrote my Lua implementation of UMENU using your method, 
where the private vars are locals of Classname.new(), as is an empty table 
called self, and setters, getters and other functions are declared inside 
Classname.new() as "self.getwhatever() return whatever end"

I agree with Dirk that this is a lot of extra work and in some cases makes 
things less obvious. But I'm a very careless and mistake prone person, and 
your method protects me from myself, kind of like wearing a helmet when I ride 
my bicycle (or when I walk and chew bubblegum at the same time). Your method 
also yields a pretty readable result.

Note that in the "class's" functions, I reference the local variables 
directly. Only from outside the "object" do I use the setters and getters.

Anyway, thanks to all of you who helped give me a great introduction to Lua.

SteveT

Steve Litt
Recession Relief Package
http://www.recession-relief.US
Twitter: http://www.twitter.com/stevelitt