[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: Re: OOP sortof
- From: Steve Litt <slitt@...>
- Date: Tue, 4 Jan 2011 03:06:12 -0500
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:
> steve code using _newindex is quite interesting! Lua once again
> surprises me (in a good way :)
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.
Recession Relief Package