Enum: create Enum class

This commit is contained in:
Van
2025-09-03 19:07:08 +00:00
parent 8cf50b90d0
commit b798ee0b76
3 changed files with 37 additions and 0 deletions

5
mods/CORE/Enum/init.lua Normal file
View File

@@ -0,0 +1,5 @@
local Enum = Mod:new()
Enum.Class = Enum.require('Enum')
Api.addModToGlobalSpace(Enum, 'Core.Enum')

2
mods/CORE/Enum/mod.conf Normal file
View File

@@ -0,0 +1,2 @@
name = smc__core__enum
description = smc__api__system

View File

@@ -0,0 +1,30 @@
---Abstract class implementing enumeration. It is a table<ENUM_KEY, ENUM_KEY>. Read-only table!
---@abstract
---@class Enum
local Enum = {}
---@param enum table[]
---@return Enum
function Enum:new(enum)
local metaEnum = {}
-- Create table<ENUM_KEY, ENUM_KEY>
for _, v in ipairs(enum) do
metaEnum[v] = v
end
---@type Enum
instance = setmetatable({}, {
__index = metaEnum,
__newindex = function()
error('The Enum table is read only!')
end,
__metatable = false
})
return instance
end
---@alias Enum.EnumKey string Key from enumeration
return Enum