lua-users wiki: Object Oriented Programming (original) (raw)
![]() |
---|
This is a collection of links related to OOP (object-oriented programming[1]) in Lua.
Learning Material
- ObjectOrientationTutorial - tutorial on OOP with classes.
- InheritanceTutorial - tutorial on OOP using inheritance[2] with classes and prototypes.
- [Chapter on OOP] from [PiL] - a comprehensive view of OOP in Lua by the language's chief-architect.
- ObjectOrientationClosureApproach - alternative approach to OOP.
Related Discussions
- ColonForMethodCall - discussion about the use of colon (
:
) instead of period (.
) for methods calls. - ObjectBenchmarkTests - performance evaluation of different approaches to access objects members.
Feature Implementations
- SimpleLuaClasses - classes with constructors.
- LuaClassesWithMetatable - classes with constructor and copy operations.
- ClassesAndMethodsExample - classes with constructors and simple inheritance.
- ObjectProperties - getters/setters that look like table field accesses (like properties in C#).
- ProxyBasedEncapsulation - encapsulation of objects via proxy tables.
- ClassesAsConstructorClosures - a flexible way of implementing classes with type testing.
Older Lua Versions
- ClassesAndMethods - classes using tagmethods of Lua 4.0.
- ClassesAndMethodsWithEventtable - classes using metatables of a pre-release version of Lua 5.0.
Feature Modules
- [lclass] - Barebones Lua with Classes (73 SLOC), inspired by Lua's lightweight design, features:
- constructor/initialization
- simple inheritance
- YetAnotherClassImplementation - support for classes with:
- constructor/initialization
- simple inheritance
- virtual methods
- access control to inherited attributes
- type casting
- MultipleInheritanceClasses - support for classes with:
- constructor/initialization
- multiple inheritance
- shared derivation (like C++ virtual)
- handling of ambiguities and inherited attributes
- ObjectLua - support for object prototyping and classes with:
- constructor/initialization
- simple inheritance
- mandatory root class
Object
- access to the originally inherited implementation in overridden methods through function
super
, similarly to Java and Python classes
- KlassModule - support for classes with:
- constructors and destructors
- simple prototype-based inheritance
isA
method- works with Lua 5.2
- [LOOP] - light-weight modules that incrementally support:
loop.base
classes with constructor/initialization supportloop.simple
classes with simple inheritanceloop.multiple
classes with multiple inheritanceloop.cached
classes with inheritance that copies members from superclasses (faster method resolution and inheritance of metamethods)loop.scoped
classes with private and protected access to members
- [lua-Coat] - port of [Coat] (a light-weight OO model of Perl5) that supports:
- constructor/initialization
- multiple inheritance
- method modifiers
- introspection operations
- attribute checking
- additional support for roles and type declarations
- [MiddleClass] - single file module (~100 LoC) that supports classes with:
- constructor/initialization
- simple inheritance
- mandatory root class
Object
- basic mixins support (copying methods)
- getter/setter construction facilities
- additional lib [MiddleClass-extras] with mixins that improve upon the basic functionality.
- [OWL] - single file module that provides a class implementation with the following features:
- constructor/initialization (can also be overridden on a per-instance basis).
- multi-level inheritance (all the way to top-level super class).
- easy-to-use with two primary functions: class() and instance()
- common OOP methods: is_a(), kind_of(), and instance_of().
- private class table (inherited by sub-classes, but not by instance objects).
- easily implement basic metamethods via add_property_callback() method.
- several samples and documentation included
- compatible with Corona SDK (provides display object classes), as well as non-Corona projects.
- [30log] - single file module. It features:
- named and unnamed classes
- constructor/initialization
- default values for properties
- single inheritance
- basic support for mixins
- [lupy] - single file module(~50 sloc, ~2 kb). It's a Python-style class implementation with several Ruby features:
- constructor/initialization
- single inheritance
- basic mixins support
- missing methods handler
- monkey patching support
- lexical scope based encapsulation
- inheritance tree based type testing
- namespace and inner class support
- [classy] - single file module (~460 sloc, ~14 kb) with:
- constructor/initialization (efficient, necessary data precomputed during class definition)
- multiple inheritance (method lookups in width-first search order)
- efficient method calls (method lookup precomputed during class definition)
- monkey patching support (updates to base classes propagate to derived classes)
- multi methods (functions with more than one polymorphic parameter)
- [lua-objects] - single file module (~300 sloc) with:
- 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
- new! default inherited class, Class
- getters and setters
- correctly handles missing methods on super classes � superCall()
- optimization (methods from super classes can be cached on class/instance)
- common methods/properties � .is_class, .is_instance, .isa(), .supers. .class
[dmc-objects] is collection oflua-object
sub-classes made for the Corona SDK. they show examples of: - custom constructor/intialization/destructor
- custom constructor/destructor names
- mixin usage, Event mixin ([lua-events-mixin]) for event capabilities (add/removeEventListener, etc)
dmc-objects
classes, several Corona apps usingdmc-objects
- [PLoop] - a powerful C# Style OOP System (~12391 sloc, ~639 kb) with:
- A full set OOP system that provide prototype, attribute, environment, enum, struct, interface, class and more.
- Full and customizable type validation, function argument validation, method overloads and etc.
- Thread pool, collection, serialization and other features for common usage.
- Frameworks for web service, mqtt service and more.
- [SIMPLOO] - Simple Lua Object Orientation (~500 sloc, ~10 kb minified):
- Use a simple syntax similar to other object-oriented languages, or use traditional lua tables.
- Define constructors and finalizers. Finalizers are called on garbage collection (also works in Lua 5.1 using newproxy).
- Keywords such as as private, public, abstract, static, const and meta (to define in-class metamethods).
- Multiple inheritance. Warns on ambiguous naming. Parent members are stored as direct references in children to keep things fast.
- Define class namespaces using a dot separated class name. Import classes from other namespaces. Define aliases for your imported classes. Uses function environments.
- A production mode config setting is included to speed up the library even more by disabling non-critical pieces of code.
- Class data is precomputed during definition. Instantiation times are O(n) where n = number of class member, due to a simple deep copy of this data.
- Namespaces are stored as nested global tables for easy access. Classes have common helper methods such as: get_name(), get_class(), instance_of().
- [oop.lua] - a support library for prototype-based programming in Lua.
- [Luaoop] - single file module, simple class-based OOP library
- multiple inheritance
- reflection
- operators
- destructor
- behavior transfer
- [POOL] - Poorman's Object-Oriented Lua
- single file module
- class template table
- constructor/destructor
- inheritance/polymorphism
- operators
Sample Implementations
- PointAndComplex - classes for points and complex numbers (uses SimpleLuaClasses).
- [LOOP Library] - class library with various OO implementations (uses [LOOP] modules):
- data strucutures (ex. memoization cache).
- debugging utilities (ex. pretty-priting, logging).
- serialization utilities (ex. streams).
- coroutine-based multithreading utilities (ex. timers, cooperative sockets, etc.).
- and other OO utilities (ex.: wrappers, publishers).
See Also
RecentChanges · preferences
edit · history
Last edited December 25, 2023 9:36 pm GMT (diff)