A lightweight and flexible object-oriented library for Lua.
- Simple class definition — Single
class()function for all OOP needs - Constructors & destructors —
initanddestroylifecycle methods - Single inheritance — Extend classes with automatic method inheritance
- Multiple inheritance — Inherit from multiple parent classes
- Method overloading — Override parent methods in child classes
- Parent method access — Call parent methods via
_parentMethod() - Ad-hoc parent calls — Pass custom parameters to parent constructors/destructors
- Singleton support — Built-in singleton pattern with
newSingleton() - Instance cloning — Create new instances from existing objects with
newInstance()
Copy luoop.lua to your project and require it:
require("luoop")require("luoop")
-- Define constructorlocalfunctionnew(self, name)
self.name=nameend-- Create classPerson=class(new)
-- Add methodsfunctionPerson:greet()
return"Hello, I'm " ..self.nameendfunctionPerson:destroy()
self.name=nilend-- Usagelocaljohn=Person("John")
print(john:greet()) -- "Hello, I'm John"john:destroy()require("luoop")
-- Parent classlocalfunctionnewAnimal(self, species)
self.species=speciesendAnimal=class(newAnimal)
functionAnimal:speak()
return"..."endfunctionAnimal:destroy() end-- Child classlocalfunctionnewDog(self, name)
self:_parentConstructor(Animal, "Canine") -- Call parent constructorself.name=nameendDog=class(newDog, Animal) -- Inherit from AnimalfunctionDog:speak() -- Override parent methodreturn"Woof!"endfunctionDog:introduce()
returnself.name.." the " ..self.speciesendfunctionDog:destroy()
self:_parentDestructor(Animal)
end-- Usagelocalrex=Dog("Rex")
print(rex:speak()) -- "Woof!"print(rex:introduce()) -- "Rex the Canine"rex:destroy()require("luoop")
-- Parent classesSwimmer=class(function(self) self.canSwim=trueend)
functionSwimmer:swim() return"Swimming!" endfunctionSwimmer:destroy() endFlyer=class(function(self) self.canFly=trueend)
functionFlyer:fly() return"Flying!" endfunctionFlyer:destroy() end-- Child inherits from bothlocalfunctionnewDuck(self)
self:_parentConstructor(Swimmer)
self:_parentConstructor(Flyer)
endDuck=class(newDuck, Swimmer, Flyer)
functionDuck:destroy()
self:_parentDestructor(Swimmer)
self:_parentDestructor(Flyer)
end-- Usagelocaldonald=Duck()
print(donald:swim()) -- "Swimming!"print(donald:fly()) -- "Flying!"print(donald.canSwim) -- trueprint(donald.canFly) -- truerequire("luoop")
localfunctionnewConfig(self)
self.debug=falseendConfig=class(newConfig)
Config:enableSingleton()
functionConfig:destroy() end-- Usagelocalconfig1=Config.newSingleton()
localconfig2=Config.newSingleton() -- Same instance as config1config1.debug=trueprint(config2.debug) -- true (same object)Config.destroySingleton() -- Clean up| Function | Description |
|---|---|
class(init, ...) | Create a new class with constructor init and optional parent classes |
| Method | Description |
|---|---|
ClassName(...) | Create new instance (calls init) |
obj:destroy(...) | Destructor (implement in your class) |
obj:newInstance(...) | Create new instance from existing object |
| Method | Description |
|---|---|
obj:_parentConstructor(Parent, ...) | Call parent's constructor with arguments |
obj:_parentDestructor(Parent, ...) | Call parent's destructor with arguments |
obj:_parentMethod(Parent, "methodName", ...) | Call parent's method |
obj:_hasParentClass(Parent) | Check if class inherits from Parent |
| Method | Description |
|---|---|
ClassName:enableSingleton() | Enable singleton mode for class |
ClassName:disableSingleton() | Disable singleton mode (before instantiation) |
ClassName:isSingleton() | Check if class is singleton |
ClassName.newSingleton(...) | Get or create singleton instance |
ClassName.destroySingleton(...) | Destroy singleton instance |
See the example/ folder for complete working examples:
basic/— Simple class definitioninheritance/— Single inheritancemultiple-inheritance/— Multiple inheritancesingleton/— Singleton pattern
- Loogine — A game engine built on Luoop
MIT License — see LICENSE for details.