Skip to content

Instantly share code, notes, and snippets.

@Quenty
Created June 22, 2020 22:05
Show Gist options
  • Save Quenty/f608d0ed76769e4796b081c2517d673e to your computer and use it in GitHub Desktop.
Save Quenty/f608d0ed76769e4796b081c2517d673e to your computer and use it in GitHub Desktop.
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