├── LICENSE ├── README.md └── enum.lua /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 Justin van der Leij 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Example: 2 | 3 | ```lua 4 | local Enum = require("enum") -- Require the library 5 | 6 | -- Create an enum either with variable amount of variables, or a table holding it. 7 | local Directions = Enum("Up", "Down", "Left", "Right") 8 | local Directions = Enum({"Up", "Down", "Left", "Right"}) 9 | 10 | -- Access a enum simply with the key. 11 | local myDirection = Directions.Up 12 | local myDirection = Directions["Up"] 13 | 14 | -- Perform logic with them 15 | if myDirection == Directions.Up then 16 | print("Were moving up, boss!") 17 | elseif myDirection == Directions.Down then 18 | print("Ship is going down!") 19 | end 20 | 21 | -- Printing enums 22 | print(myDirection.Up) -- 'Up' 23 | print(Directions.Down) -- 'Down' 24 | 25 | for key, value in pairs(Directions) do 26 | print(key.. " : " ..value) 27 | end 28 | --[[ -- In no predefined order: 29 | 'Up : Up' 30 | 'Down : Down' 31 | 'Left : Left' 32 | 'Right : Right' 33 | ]] 34 | 35 | -- Some nice to knows 36 | local Directions = Directions.Sideways -- Error: Attempt to index non-existant enum 'Sideways'. 37 | Directions.foo = "bar" -- Error: "Attempt to write to static enum" 38 | ``` 39 | -------------------------------------------------------------------------------- /enum.lua: -------------------------------------------------------------------------------- 1 | local Enum = {} 2 | local Meta = { 3 | __index = function(_, k) error("Attempt to index non-existant enum '"..tostring(k).."'.", 2) end, 4 | __newindex = function() error("Attempt to write to static enum", 2) end, 5 | } 6 | 7 | function Enum.new(...) 8 | local values = {...} 9 | 10 | if type(values[1]) == "table" then 11 | values = values[1] 12 | end 13 | 14 | local enum = {} 15 | 16 | for i = 1, #values do 17 | enum[values[i]] = values[i] 18 | end 19 | 20 | return setmetatable(enum, Meta) 21 | end 22 | 23 | return setmetatable(Enum, { 24 | __call = function(_, ...) return Enum.new(...) end, 25 | }) 26 | --------------------------------------------------------------------------------