[Deprecated]
I now prefer to use native prototype-based object oriented programming, in both Lua and JavaScript.
For inspiration, you can look at these JS refs:
- the Objects Linked to Other Objects (OLOO) part of the New Rules for JavaScript (video) presentation,
- JS Inheritance is awesome, and you're doing it wrong, blog post.
And of course, the historical papers about prototypical objects:
- Using Prototypical Objects to Implement Shared Behavior in Object Oriented Systems, Henry Lieberman - OOPSLA '86,
- Classes versus Prototypes in Object-Oriented Languages (pdf), Alan Borning - 1986,
- Class Warfare: Classes vs. Prototypes, Brian Foote - OOPSLA '89,
- Organizing Programs Without Classes (pdf), UNGAR, CHAMBERS, CHANG, HÖLZLE - 1991.
A class-oriented OOP module for Lua
This is an implementation of a class-oriented Lua module, coded entirely in Lua. The object model allows you to override new()
(see tests), so you can write classes that behave the way you want. While still in design stage, the interface is getting quite stable and I will add a multiple inheritance mechanism if a user asks me to, or if I need it miself.
This is a beta release, please report bugs & feature requests to the project's UserVoice Page.
- Class oriented
- All classes derive from Object
- Single inheritance (will add multiple if someone needs it)
- All method calls are virtual, the class of the instance determines the bound method
- Access to superclass method in overriden methods with a Java-like
super.method(self)
- The use of super only grants access to methods, NOT to data members (prevents many evils)
- Consistent use of
:
, always use:
to call any method of any class or instance - Object model allows to redefine
new()
in any class (see tests) - Use
has
to define members (inspired by Moose) - Hopefully only one way to do things, very predictible results
Note: there is a Prototype.lua
file in the package, which is completely independant, but if you need prototype-based oop have a look at it (it provides a super(self)
function).
- This is a beta release. Changes can be made in the future
- No optimization. This is still in the design stage
This project's code can be dowloaded from its GitHub page.
You can also install ObjectLua from LuaRocks:
$ luarocks install objectlua
See TestObject.
The tests documenting ObjectLua usage can be found in the test
directory. You can run them with:
$ rake test
They use LuaUnit.
- PIL, by Roberto Ierusalimschy.
- The Lua Wiki.
- Open Reusable Object Models, by Ian Piumarta & Alessandro Warth, for bootstraping the object model.
- This article by John Resig for one of the previous implementations of super().
- This exchange between Guido van Rossum (Python) and Jim Althoff (Smalltalk) for the object model.
- Traits: Composable Units of Behavior by Nathanael Schärli, Stéphane Ducasse, Oscar Nierstrasz and Andrew Black for Traits
The main point is to bootstrap the object model and then,
build on it to populate Class
with new()
, inheritsFrom()
, subclass()
,
and Object
with initialize()
, isKindOf()
, clone()
...
Thanks to Ludovic Perrine for making things clearer with enlightening discussions. Thanks to Robert Rangel for finding a blocker bug fixed in version 0.0.3.
- The project's UserVoice page for bugs & feature requests.
- The project's GitHub page
- ObjectOrientedProgramming