Skip to main content
gamedev.net
🔒 Locked

Does Lua support Object-Oriented programming?

Started by michaelj Dec 6, 2005 at 9:20 PM 11 replies 2.1k views
Original Post
michaelj
michaelj
I am wondering if Lua have internal support for OO programming?
Sneftel
Sneftel
Lua support for classes is closer to Python--offering features which can be used for OOP, including a bit of OOP-specific syntactic sugar--than it is to a language such as C++ or SmallTalk which embeds OOP at the lowest levels of syntax.
michaelj
michaelj
Just like long ago that people use C to write OO programs?

Then does it means that it would be a hard work to use OOP with Lua?
NoodleCower
NoodleCower
Not neccessarily.

You can do something like this, for example:

myClass = {}function myClass:foo( ) -- self is a reference to the table that stores the function  self:bar( "Pie" ) -- call bar from selfendfunction myClass:bar( a )  print( a )endfunction myClass:create( )  ret = {}  ret.someValue = nil  ret.foo = myClass.foo  ret.bar = myClass.bar  return retenditem = myClass:create( )item:foo( ) -- automatically passes 'item' as 'self' (read above)


1:30am, so bear with me if the syntax is a bit off or something. What I'm saying is that although Lua is primarily a straight-forward procedural language, you can use OO features via its 'syntactic sugar' features (which I have to admit I'm quite happy with).

I hope that helps you :)
chot
chot
its actually quite easy to use lua for oop. in my engine i have writen a function called class, which creates an object that can be considered (and used as) a class. then all you got to do is create methods. it looks like this:

my_class = class()function my_class:create(x) local o = my_class.__parent.create(self) o.i = x return oendfunction my_class:dump() print("i is set to" .. self.i .. " in this object")endmy_object = my_class:create(7)my_object:dump()


and that would actually be valid lua code
----------Thale Cres
sindisil
sindisil
Quote:
Original post by Sneftel
Lua support for classes is closer to Python--offering features which can be used for OOP, including a bit of OOP-specific syntactic sugar--than it is to a language such as C++ or SmallTalk which embeds OOP at the lowest levels of syntax.

How is Python less object oriented that C++?! If anything, it is *more* so, since numbers themselves are objects, though both Python and C++ take a multi-paradigm approach, allowing for several modes of programming: OO, modular, procedural -- even somewhat functional, in the case of Python.

A better, in fact nearly perfect, comparison would be with Perl. Both build OO on top of dictionarys (hashes in Perlish) with little to no syntactic sugar.

Roboguy
Roboguy
Quote:
Original post by sindisil
Quote:
Original post by Sneftel
Lua support for classes is closer to Python--offering features which can be used for OOP, including a bit of OOP-specific syntactic sugar--than it is to a language such as C++ or SmallTalk which embeds OOP at the lowest levels of syntax.

How is Python less object oriented that C++?! If anything, it is *more* so, since numbers themselves are objects, though both Python and C++ take a multi-paradigm approach, allowing for several modes of programming: OO, modular, procedural -- even somewhat functional, in the case of Python.

A better, in fact nearly perfect, comparison would be with Perl. Both build OO on top of dictionarys (hashes in Perlish) with little to no syntactic sugar.


Maybe I'm missing something, but I don't think he said it was less object oriented than C++...
sindisil
sindisil
Not that I'm interrested in a "pureness" flame war, but that's exactly what he implied by saying that Python didn't embed OOP at the lowest levels of syntax. I'm just trying to avoid mischaracarization of Python, that's all.
ldeej
ldeej
Actually support for OOP is very different in Python than in Lua.

In Python the language has a real object system, in which inheritance, virtual functions, objects, and classes are natively supported.

In Lua, OOP is emulated using tables (which is the "only" native complex data type Lua has), and inheritance and others are emulated via metatables. Lua provides syntax sugar for such emulation, but at the center of the system there are no such things are objects, only Tables.
Roboguy
Roboguy
Quote:
Original post by sindisil
Not that I'm interrested in a "pureness" flame war, but that's exactly what he implied by saying that Python didn't embed OOP at the lowest levels of syntax. I'm just trying to avoid mischaracarization of Python, that's all.


To me that just implies that the OO system is implemented differently not that it's less powerful. I really doubt he meant what you seem to think he meant. In fact, you were the only one who directly said that one language's OO system is (or might be) more powerful than another's.
ldeej
ldeej
But one system might be more powerful than others in this case :-), and as with most in Computer Science no tool is better than another for the general case, tools are better than others for specific situations.

Lua's support for OOP is emulated, stuff like interfaces, virtual functions, and inheritance need support from the Lua framework provided by the programmer in order for them to work as expected.

Python has support for native OOP, which removes such burden from the programmer for those cases.

Is Python better than Lua then? Of course not, the answer depends on several things (i.e. programmer familiarity with the language, company issues with legal bindings on the language licenses, target platform, final goal, etc, etc, etc)

With no restrictions other than the final goal of the application I would choose the following:
If I were to create a complex class hierarchy using inheritance, interfaces, composition, complex design patterns, etc. I would choose Python since I know I would now have to worry about setting and re-setting metatables for function redirection, for inheritance emulation, etc.

If I were to deal with a flat class hierarchy, or just do generic object management I would choose Lua.

For Game Development at the script level, it is usually not necessary to create a complex OO hierarchy to drive the game. So in that sense both languages provide good support for what needs to be achieved.

To be clear, it is not that the OO systems are implemented differently since there is no OO system in Lua. (as someone suggested before C programmers have been doing OO based programming for a while without OOP support), this is the case with Lua, it is possible to emulate OOP but it is not really part of the language.

To answer the question in the original post:
Is there OO programming support in Lua?
Yes it is possible to emulate different OOP features in Lua, and in reality is not hard to use. If you are trying to do some complex OOP in Lua you are likely to encounter some unique challenges in order to implement them since in this case the burder falls in the programmer.
Lua provides syntatic sugar for accessing tables as passing implicit parameters to emulate OOP, which is more than enough for simple cases.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.