| Created | Edited |
|---|---|
2025-04-21T19:28 |
2025-04-21T22:36 |
This module provides a utility for creating and managing classes in Lua, designed for use in MediaWiki environments. It supports inheritance, static methods, metamethods, and property accessors.
- Class Creation: Define classes with constructors, methods, and properties.
- Inheritance: Support for parent-child class relationships.
- Static Members: Define static methods and properties.
- Metamethods: Customize behavior with Lua metamethods (e.g.,
__add,__eq,__index). - Property Accessors: Use
getandsetfor controlled property access.
local makeClass = require("Module:Class")local Parent = makeClass("Parent", {
constructor = function(self)
-- Initialization logic
end,
static = {
test = {
set = function(self, v)
error(v, 0)
self._test = v
end,
get = function(self)
return 0
end,
},
}
})
local Class = makeClass("Class", {
parent = Parent,
constructor = function(self)
self:super()
return {}, 0
end,
{
get = function()
return 8
end,
},
_test = 6,
__add = function(a, b)
return a.test + b
end,
static = {
test = function(self)
Parent:checkSelfStatic(self)
end,
},
})The following test cases demonstrate the module's functionality:
function p.test()
local Parent = makeClass("Parent", { ... })
local Class = makeClass("Class", { ... })
local Test = Class:childClass{
constructor = function(self, ...)
return self:super()
end
}
local c = Parent()
for i = 1, 10000 do
local c = Test()
end
return mw.dumpObject(#helpers.tableKeys(instanceRegistry))
endThe module is optimized for performance, as demonstrated in the test case where 10,000 class instances are created efficiently.
This module is provided under the MIT License. See the LICENSE file for details.
Contributions are welcome! Please submit issues or pull requests via the repository.