Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Enums in Lua (library)
-- -- Go to the bottom for a sample -- do -- nilString local funcs = {} local envMt = { __index = function(self, key) local fo = funcs[key] if fo then fo.before() return function(...) local returning = fo.func(...) fo.after(...) return returning end end return nil end, } setmetatable(_ENV, envMt) local nilStringMt = { __index = function(self, key) return envMt.__index(self, key) or key end, } local function startNilString() setmetatable(_ENV, nilStringMt) end local function stopNilString() setmetatable(_ENV, envMt) end -- enum item local enumItem = {} enumItem.__index = enumItem enumItem.__tostring = function(self) return ("(EnumItem %s)"):format(self.name) end enumItem.__eq = function(self, comparing) return self.value == comparing.value end function newEnumItem(value, from, name) return setmetatable({ value = value, name = name, __from = from, }, enumItem) end -- enum class local enum = {} enum.__index = function(self, k) return self.__keys[k] or enum[k] end local function newEnum(tbl) local instance = {__keys = {}} for keyCount, key in ipairs(tbl) do instance.__keys[key] = newEnumItem(keyCount, instance, key) end return setmetatable(instance, enum) end -- enum func local enumFunc = {} funcs.Enum = enumFunc function enumFunc.before() startNilString() end function enumFunc.func(tbl) return newEnum(tbl) end function enumFunc.after() stopNilString() end end -- -- Sample -- Errors = Enum { StringEmpty, Success } function say(str) if str == "" then return Errors.StringEmpty end print(str) return Errors.Success end local Success = say("") if Success == Errors.StringEmpty then print("String empty!") return end print("Success!")
run
|
edit
|
history
|
help
0
Random number generation with ranges (Lua)
Beispiel2
Kegs Scores ... FOOOOOOTBALLLLLL
cases
Key
Basic class example
test123
test123
Simple Lua primality test
kraft1c