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

Excellent!

I just thought of another benefit of this: If you misspell a setter or getter, 
you get an error message. If you misspell a key to a table, the program 
continues and you get a computational error or a crash much later. I think I'm 
going to use your technique in my Lua version of UMENU.

On the self-documentation front, I like the way you call the local table 
inside the new() method "self". That makes the code much more understandable 
for a newbie like me.

> Notice that it pretty much makes the a.foo(a,...) and a:foo(...)
> unecessary but I left it to conform with your example.

Lucas, I didn't understand exactly what you meant in the preceding sentence 
until reading the doc you put down below. Now I understand.

> 
> Additional reading on the subject can be done here:
> http://lua-users.org/wiki/ObjectOrientationClosureApproach

Fascinating! And rather Rubyesque. :-)


My one qualm about that is that a person not familiar with what you're doing 
will not understand why you use dot instead of colon. It could prove 
problematic for the maintenance programmer. But that's nothing that some 
decent documentation couldn't solve.

> 
> PS.:
> 
> steve code using _newindex is quite interesting! Lua once again
> surprises me (in a good way :)

Out-Standing!!!

So in other words, after keeping x and y as locals in the surrounding code 
instead of keys of self, I can use __newindex to prevent the application 
programmer from setting self.x or self.y (which would not be the same x and y 
as set by setX and setY. If I wanted to, I could even use _index to prevent 
read access.

All this stuff is overkill for small programs and quick prototypes, but can be 
very helpful when writing bigger programs.

I'm about to refactor my (nonworking umenu.lua), and I think I'm going to use 
your technique for the item and menu "classes".

Thanks

SteveT

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