Skip to content

Thundercraft5/Class.lua

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Created Edited
2025-04-21T19:28
2025-04-21T22:36

Module:Class

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.

Features

  • 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 get and set for controlled property access.

Usage

Importing the Module

local makeClass = require("Module:Class")

Example

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,
    },
})

Test Cases

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))
end

Performance

The module is optimized for performance, as demonstrated in the test case where 10,000 class instances are created efficiently.

License

This module is provided under the MIT License. See the LICENSE file for details.

Contributing

Contributions are welcome! Please submit issues or pull requests via the repository.

About

Performant Class Library for Lua

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages