Releases | Download | LuaRocks | Documentation | API | Issues | Contributing
Class is a lightweight object-oriented helper for Lua projects that need class creation, instance initialization, inheritance-style includes, private instance state, accessor helpers, cloning, operator helpers, and debug output in a single class.lua file.
It is designed for projects that want OOP ergonomics without pulling a framework into the runtime.
- Single file : Drop
class.luainto your project or install it with LuaRocks. - No runtime dependency : Pure Lua, no framework required.
- Composable classes : Reuse behavior with inheritance-style includes.
- Private instance state : Store internal data through
self.__private. - Useful helpers : Accessors, cloning, operators, and debug tools.
- Lua 5.1+ compatible : Tested across Lua 5.1, 5.2, 5.3, and 5.4.
luarocks install lost-classThen require the module:
local mClass = require("class")The LuaRocks package is named
lost-class, but the Lua module remainsclass.
Copy class.lua into your project:
cp class.lua path/to/your/project/class.luaThen require it:
local mClass = require("class")local mClass = require("class")
local cPlayer = mClass({
__type = "Player",
init = function(self, sName)
self.name = sName
self.__private.score = 0
end,
addScore = function(self, nAmount)
self.__private.score = self.__private.score + nAmount
end,
getScore = function(self)
return self.__private.score
end,
})
local oPlayer = cPlayer("Ada")
oPlayer:addScore(10)
print(oPlayer.name)
print(oPlayer:getScore())Expected output:
Ada
10Includes let you compose behavior from another class-like definition.
local mClass = require("class")
local cNamed = mClass({
getName = function(self)
return self.name
end,
})
local cUser = mClass({
__includes = cNamed,
init = function(self, sName)
self.name = sName
end,
})
print(cUser("Ada"):getName())| Feature | Description |
|---|---|
| Class creation | Build callable classes from Lua tables. |
| Instance initialization | Define an init method for constructor-like behavior. |
| Includes | Share methods between class definitions. |
| Private state | Use self.__private for per-instance internal data. |
| Accessors | Generate and manage structured access to values. |
| Cloning | Duplicate class instances when needed. |
| Operators | Add helper behavior for Lua metamethod-driven patterns. |
| Debug output | Inspect class and instance information more easily. |
The documentation is available here:
https://lost-things-studio.github.io/Class/
Run it locally:
cd docs
npm install
npm run startBuild the static site:
cd docs
npm run buildThe generated site is written to docs/build.
Run the Lua test suite from the repository root:
lua tests/run.luaCheck syntax:
luac -p class.lua tests/*.luaThe GitHub Actions test workflow runs on Lua 5.1, 5.2, 5.3, and 5.4.
The latest release is available here:
https://github.com/Lost-Things-Studio/Class/releases/latest
Contributions are welcome.
Please read:
Class is released under the MIT License.
