Created
June 22, 2020 22:05
-
-
Save Quenty/f608d0ed76769e4796b081c2517d673e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
local function readonly(_table) | |
return setmetatable(_table, { | |
__index = function(self, index) | |
error(("Bad index %q"):format(tostring(index)), 2) | |
end; | |
__newindex = function(self, index, value) | |
error(("Bad index %q"):format(tostring(index)), 2) | |
end; | |
}) | |
end | |
--- Returns whether `value` is within `table` | |
-- @tparam table table to search in for value | |
-- @param value Value to search for | |
-- @treturn boolean `true` if within, `false` otherwise | |
local function contains(_table, value) | |
for _, item in pairs(_table) do | |
if item == value then | |
return true | |
end | |
end | |
return false | |
end | |
local weatherTypes = readonly({ | |
DYNAMIC = "dynamic"; | |
SNOW = "snow"; | |
RAIN = "rain"; | |
NONE = "none"; | |
}) | |
print(table.find(weatherTypes, "rain")) --> nil | |
print(contains(weatherTypes, "rain")) --> true |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment