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 constructor
local function new(self, name)
self.name = name
end
-- Create class
Person = class(new)
-- Add methods
function Person:greet()
return "Hello, I'm " .. self.name
end
function Person:destroy()
self.name = nil
end
-- Usage
local john = Person("John")
print(john:greet()) -- "Hello, I'm John"
john:destroy()require("luoop")
-- Parent class
local function newAnimal(self, species)
self.species = species
end
Animal = class(newAnimal)
function Animal:speak()
return "..."
end
function Animal:destroy() end
-- Child class
local function newDog(self, name)
self:_parentConstructor(Animal, "Canine") -- Call parent constructor
self.name = name
end
Dog = class(newDog, Animal) -- Inherit from Animal
function Dog:speak() -- Override parent method
return "Woof!"
end
function Dog:introduce()
return self.name .. " the " .. self.species
end
function Dog:destroy()
self:_parentDestructor(Animal)
end
-- Usage
local rex = Dog("Rex")
print(rex:speak()) -- "Woof!"
print(rex:introduce()) -- "Rex the Canine"
rex:destroy()require("luoop")
-- Parent classes
Swimmer = class(function(self) self.canSwim = true end)
function Swimmer:swim() return "Swimming!" end
function Swimmer:destroy() end
Flyer = class(function(self) self.canFly = true end)
function Flyer:fly() return "Flying!" end
function Flyer:destroy() end
-- Child inherits from both
local function newDuck(self)
self:_parentConstructor(Swimmer)
self:_parentConstructor(Flyer)
end
Duck = class(newDuck, Swimmer, Flyer)
function Duck:destroy()
self:_parentDestructor(Swimmer)
self:_parentDestructor(Flyer)
end
-- Usage
local donald = Duck()
print(donald:swim()) -- "Swimming!"
print(donald:fly()) -- "Flying!"
print(donald.canSwim) -- true
print(donald.canFly) -- truerequire("luoop")
local function newConfig(self)
self.debug = false
end
Config = class(newConfig)
Config:enableSingleton()
function Config:destroy() end
-- Usage
local config1 = Config.newSingleton()
local config2 = Config.newSingleton() -- Same instance as config1
config1.debug = true
print(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.