local enum = {} local len_key = "len" local _ = {} setmetatable(_, {__mode = "k"}) function enum:__len() local i = 0 for k, v in pairs(_[self]) do if k ~= len_key then i = i + 1 end end return i end function enum:new(o) local t = {} _[t] = {[len_key] = enum.__len} for k, v in pairs(o) do if tonumber(k) ~= nil then k, v = v, k - 1 end if _[t][k] ~= nil then error('"' .. k .. '" can not be set.', 2) end _[t][k] = v end return setmetatable(t, self) end function enum:__index(k) if _[self][k] == nil then error('"' .. k .. '" is undefined enumerator.', 2) end return _[self][k] end function enum:__newindex() error("enum is read-only.", 2) end return enum
enum = require "enum" test = enum:new{"aa", "bb", cc = 100} print(test.aa) --> 0 print(test.bb) --> 1 print(test.cc) --> 100 print(#test) --> 0 (Lua5.1) or 3 (Lua5.2) print(test:len()) --> 3 --print(test.dd) --> error : "dd" is undefined enumerator. --test.aa = 10 --> error : enum is read-only.
function switch(c) local swtbl = { casevar = c, caseof = function (self, code) local f if (self.casevar) then f = code[self.casevar] or code.default else f = code.default end if f then if type(f)=="function" then return f(self.casevar,self) else error("case "..tostring(self.casevar).." not a function") end end end } return swtbl end
require "switch" c = 1 switch(c) : caseof { [1] = function (x) print(x,"one") end, [2] = function (x) print(x,"two") end, [3] = 12345, -- エラー default = function (x) print(x,"default") end, } c = "dde" switch(c) : caseof { ['abc'] = function (x) print(x,"c is abc") end, ['def'] = function (x) print(x,"c is def") end, ['ddd'] = 12345, -- エラー default = function (x) print(x,"default") end, }
::redo1:: for x=1,10 do for y=1,10 do if not f(x,y) then goto continue1 end if not g(x,y) then goto skip1 end if not h(x,y) then goto redo1 end ::continue1:: end end ::skip1:: ::redo2:: for x=1,10 do for y=1,10 do if not f(x,y) then goto continue2 end if not g(x,y) then goto skip2 end if not h(x,y) then goto redo2 end ::continue2:: end end ::skip2::
// C++の3項目演算 x = a ? b : c
-- Luaの3項演算っぽい記述方法 x = a and b or c
local cond = 3 local x = cond==3 and "三" or "他" print(x)