lua-users home
lua-l archive

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


On Sun, 23 Feb 2014 11:19:58 +0900
"Journeyer J. Joh" <oosaprogrammer@gmail.com> wrote:

> Hi,
> 
> I am almost newbie about lua. Actually I've read the book -
> Programming in Lua and experienced Orbit, Orbiter. But that is about
> it.
> 
> I want to learn programming skill in the OO style.
> Firstly, I want to try to implement GoF Patterns with lua.
> 
> To do this, I've read chapters about OO programming from the book
> above. But I think the book didn't mention about virtual function.
> 
> But I found some materials from the url below.
> 
> http://lua-users.org/wiki/SampleCode
> 
> Among several materials above, there are two in my eyes,
> YetAnotherClassImplementation<http://lua-users.org/wiki/YetAnotherClassImplementation>
> ObjectLua <http://lua-users.org/wiki/ObjectLua>
> 
> But before I go further by myself, I'd like to listen some advice from
> experienced lua programmers.
> 
> What is the best way to learn about how to program OO style lua?
> What method would be the best for OO style lua programming?
> 
> I use lua 5.1

Hi Journeyer,

First, thank you for making me aware of GoF
(http://en.wikipedia.org/wiki/Design_Patterns). I wish you'd told me
about that book in 1994 :-). From what Wikipedia says, this must be a
good book. And I *thoroughly* agree to emphasize composition and
aggregation over inheritance.

If I read your post correctly, your goal is to implement GoF patterns,
and you've chosen Lua. However, the way your email reads, it could
be that you're already expert with GoF patterns and now just want to
apply them to Lua.

If the former, Lua is an excellent choice because Lua is like a Comp
Sci program in a box (in a download, actually). All the exotic stuff
you read about in Comp Sci books can be done straightforwardly in Lua.
And if I read the Wikipedia page about GoF right, their stress is as
much about design patterns assisted by OOP as on OOP itself. The
Wikipedia page goes so far as to say that, using composition,
aggregation, and delegation (callback routines), you can make any
language, including C, act like OOP. That rings true. With structs
including function pointers, you pretty much have OOP in C.

Every bit of what's in the preceding paragraph is easy and
understandable in Lua.

If you already have the design pattern skills and just want to apply
them to Lua, in your case I might take all those patterns and apply
them individually to Lua, and then decide what you think is the best OO
implementation for *you* to use.

As far as virtual functions, as I remember from my (hated) C++, virtual
functions exist so the program can decide at runtime what type of
object it's working on, so that if babyduck inherits from duck inherits
from animal inherits from object, when you pass babyduck to an object
that can take an argument of any descendant of object, the function
knows to say "peep", not "quack" or no answer or "no talk() method".
As I remember, the whole virtual function thing was in order to
work around C++ strong typing, and Lua doesn't have strong typing, so
I'm pretty sure that in Lua, the function would just "do the right
thing" at runtime.

Truth be told, the hoops you had to go through to make C++ decide an
object's type, (and therefore the right methods), at runtime, was why I
finally just gave up on C++. Strong typing is great with simple and
composite types, but it really starts making a mess of things when
dealing with objects and callback routines.

Please keep us in the loop as you progress in applying GoF to Lua.

Thanks,

SteveT

Steve Litt                *  http://www.troubleshooters.com/
Troubleshooting Training  *  Human Performance