Advanced object oriented module for Lua (OOP)
This single-file module started its life as dmc-objects and was used to create mobile apps built with the Corona SDK. It was later refactored into two files lua_objects.lua & dmc_objects.lua so that pure-Lua environments could benefit, too (eg, lua-corovel).
This power-duo have been used to create relatively complex Lua mobile apps (~60k LOC), clients for websockets and the WAMP-protocol, and countless others.
- new! customizable methods and names for constructor/destructor
- new! multiple inheritance (all way to top level)
- new! handles ambiguities of inherited attributes
- new! advanced support for mixins
- getters and setters
- correctly handles missing methods on super classes
- optimization (copy methods from super classes)
- new! unit tested
Here's a quick example showing how to create a custom class.
--== Import modulelocalObjects=require'dmc_lua.lua_objects'--== Create a classlocalAccountClass=newClass()
--== Class PropertiesAccountClass.DEFAULT_PATH='/path/dir/'AccountClass.DEFAULT_AMOUNT=100.45--== Class constructor/destructor-- called from obj:new()functionAccountClass:__new__( params )
params=paramsor {}
self._secure=params.secureortrueself._amount=params.amountorself.DEFAULT_AMOUNTend-- called from obj:destroy()functionAccountClass:__destroy__()
self._secure=nilself._amount=nilend--== Class getters/settersfunctionAccountClass.__setters:secure( value )
assert( type(value)=='boolean', "property 'secure' must be boolean" )
self._secure=valueendfunctionAccountClass.__getters:secure()
returnself._secureend--== Class methodsfunctionAccountClass:deposit( amount )
self._amount=self._amount+amountself:dispatchEvent( AccountClass.AMOUNT_CHANGED_EVENT, { amount=self._amount } )
endfunctionAccountClass:withdraw( amount )
self._amount=self._amount-amountendAnd here's how to work with that class.
-- Create instancelocalaccount=AccountClass:new{ secure=true, amount=94.32 }
-- Call methodsaccount:deposit( 32.12 )
account:withdraw( 50.00 )
-- optimize method lookupobj:optimize()
obj:deoptimize()
-- Check class/object types assert( AccountClass.is_class==true ), "AccountClass is a class" )
assert( AccountClass.is_instance==false ), "AccountClass is not an instance" )
assert( obj.is_class==false, "an object instance is not a class" ) assert( obj.is_instance==true, "an objects is an instance of a class" )
assert( obj:isa( AccountClass ) ==true, "this obj is an instance of AccountClass" )
-- Destroy instanceaccount:destroy()
account=nilThe project dmc-objects contains two lua-objects sub-classes made for mobile development (ObjectBase & ComponentBase). These sub-classes show how to get more out of lua_objects, such as:
- custom initialization and teardown
- custom constructor/destructor names
- custom Event mixin (add/removeListener/dispatchEvent) lua-events-mixin
You can even customize the names used for construction and destruction.
-- use 'create' instead of 'new'-- eg, MyClass:create{ secure=true, amount=94.32 }--registerCtorName( 'create' )
-- use 'removeSelf' instead of 'destroy'-- eg, obj:removeSelf()--registerDtorName( 'removeSelf' )