Multiple Inheritance Classes

lua-users home
wiki

A multiple-inheritance class library for Lua

This library is based on the general guidelines on OOP in Lua found in the PIL (Programming in Lua by Roberto Ierusalimschy) and it is also inspired by the class library in SimpleLuaClasses, which was taken as a starting point.

The idea is to implement a full-featured class system in C++ style, including multiple inheritance, shared (like C++ virtual) derivation and the correct handling of ambiguities and inherited attributes.

Summary

Brief summary using global named classes (see manual).

Standard derivation

class.A(P, Q)
class.B(P, Q)
class.C(A, B)

Cobj = C(constructor arguments ...)

Shared derivation

class.X(P, shared(Q))
class.Y(P, shared(Q))
class.Z(X, Y)

Zobj = Z(constructor arguments ...)

Class usage

class.Account()
function Account:__init(initial)  -- constructor
	self.balance = initial or 0
end
function Account:deposit(amount)
	self.balance = self.balance + amount
end

myAccount = Account(10.00)

class.NamedAccount(Account)
function NamedAccount:__init(name, initial)
	self.Account:__init(initial)
	self.name = name or 'anonymous'
end

myNamedAccount = NamedAccount('John', 10.00)

Object properties live in their respective objects.

myNamedAccount.name		== 'John'
myNamedAccount.balance		== nil 
myNamedAccount.Account.balance	== 10.00

Unambiguous base attributes are inherited. These two are equivalent:

myNamedAccount.Account:deposit(2.00) 
myNamedAccount:deposit(2.00)

Ambiguous attributes are not inherited:

class.SavingsAccount(Account)
class.CurrentAccount(Account)
class.CombinedAccount(SavingsAccount, CurrentAccount)

myCombinedAccount = CombinedAccount()
myCombinedAccount:deposit(2.00)  ← Error, deposit is nil

Qualification solves ambiguity:

myCombinedAccount.CurrentAccount:deposit(2.00)

There would exist a CombinedAccount:deposit() method if SavingsAccount and CurrentAccount derived from shared(Account). But it does not make sense in this case, since we really need two separate balances.

Files

Complete Word documentation and source code can be found here:

[Manual]
[Brief summary - named classes]
[Brief summary - unnamed classes]
[Source - named and unnamed classes]
[Source - only unnamed classes]

These are some simple examples for single and multiple inheritance:

[Single inheritance, unnamed classes]
[Multiple inheritance, unnamed classes] (1)
[Single inheritance, named classes]
[Multiple inheritance, named classes] (1)
[Multiple inheritance, named classes]

(1) In these examples you may play with options shared_meters (which enables shared inheritance) and keep_ambiguous (the latter must be defined before require).

These two simple classes illustrate indexing and metamethods:

[A tuple class with indexing]
[A set class with metamethods]

Everything together:

[Archive]

Lua files were edited with 4-space tabs.

Authors

Hugo Etchegoyen
hetchegoyen@hasar.com

Please add your data here if you modify this, thank you.

History

Version 2.04.04 - November 15, 2010
Included patches and ideas from Peter Schaefer (peter.schaefer@gmail.com) 
improving the efficiency of build(), __init() and other parts of the code.
The former remove_ambiguous() function was removed, improving the efficiency 
of both build() (instance creation) and mt:__call() (class creation). 
Ambiguities are now processed using the ambiguous_keys tables introduced by Peter.
Removed inheritance of class properties, which was inconsistent with
the way instance properties are handled (see pages 4-5 of the manual).
This was mostly harmless, but confusing. Now only methods are inherited.

Version 2.03 - February 6, 2007
Added support for indexing via __get() and __set() methods.
Added a couple of examples illustrating indexing and metamethods.

Version 2.02 - January 31, 2007
Added unclasslib.lua, a more efficient version of classlib.lua restricted
to unnamed classes.
Added compatibility with versions of Lua compiled without old-style vararg
support.

Version 2.01 - January 30, 2007
Added named classes and dot notation. Replaced the previous page
which was too long by a summary, details to be found in the
documentation, get it by clicking on the link above.
This version is backwards compatible with unnamed classes and the
square bracket notation, so it should run any previous code.

Version 1.03 - January 26, 2007
Changed the handling of constructors so that base objects not
explicitly initialized by them still get initialized by default.
Removed the source code from this page. Get it by clicking on 
the link above.

Version 1.02 - January 25, 2007
Added the possibility of not deleting ambiguous values from
classes and objects, useful for debugging.
Added a couple of simple test examples.

Version 1.01 - January 24, 2007
Minor polishing - modified some for loops to use ipairs()

Version 1.0 - January 24, 2007

See Also


RecentChanges · preferences
edit · history
Last edited November 18, 2010 4:20 pm GMT (diff)