[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Using environment for OO
- From: "Martin C. Martin" <martin@...>
- Date: Fri, 10 Apr 2009 13:24:53 -0400
Hi,
A standard approach to OO in Lua is to pass the "self" object as the
first argument. Lua has syntactic support for this with the : operator,
and it follows what languages like Java & C++ do. This means that you
can refer to globals using "a" and fields of the object as "self.a".
Another option is to use the object (i.e. table) as the environment of
the function. Then you refer to fields using "a", and globals using
"_G.a". It would be easy enough to set up "global" as a synonym for "_G".
For an OO style while accessing fields is more common than globals, the
environment option seems cleaner than the standard : option. Are there
any down sides that I'm missing?
Best,
Martin