lua-users home
lua-l archive

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



> -----Original Message-----
> From: lua-l-bounces@lists.lua.org [mailto:lua-l-bounces@lists.lua.org] On
> Behalf Of Journeyer J. Joh
> Sent: zondag 23 februari 2014 3:20
> To: Lua mailing list
> Subject: How can I learn about Object Oriented programming with lua?
> 
> 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 
> ObjectLua
> 

I think stdlib and Penlight both also have their own object implementations. For learning I think LOOP (http://loop.luaforge.net/) is your best start.

> 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?

Lua provides the means to do OO, but that also means that you can do it in many different ways. (have a look at LOOP, which implements a bunch of them)

Understand the table as being the core of an object
Understand __index metamethod
Understand difference between 'fat' inheritance (copying all members of a class into a new table), versus 'dynamic' using the __index metamethod (a change to an ancestor might change all descendants)
Understand 'self' (eg. Calling a function with the ':' instead of the '.')

To really learn, build yourself some object systems using single inheritance, multiple, private members, etc.

> What method would be the best for OO style lua programming?

None. Depends on your own needs, sometimes strict inheritance is best (build a class and then instantiate it, even for the smallest changes), while at other times you can combine some inheritance with simple assigning functions to a member and hence overriding them (so it's not strictly defined as a class), or even no inheritance at all, just a table with functions that organizes code in an object like way. When coding you should ask yourself whether you need to use the OO paradigm, quite often I find myself using a table to organize code in an object like way, without any inheritance whatsoever, simply because the code is not reused elsewhere.

Hth
Thijs

> 
> I use lua 5.1
> 
> Sincerely
> Journeyer
> 
> ----------------------------------------
> Journeyer J. Joh
> o o s a p r o g r a m m e r
> a t
> g m a i l  d o t  c o m
> ----------------------------------------