lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


>> • feat: utils.enum added to create enums and prevent magic strings #408
> 
> I'd like to see an example of the intended use for it (yes, we do
> overuse magic strings, which is efficient because of interning in Lua)
> 
> steve d.

Hi Steve,
From the documentation:


-- accessing at runtime

local obj = {}
obj.MOVEMENT = utils.enum("FORWARD", "REVERSE", "LEFT", "RIGHT")


if current_movement == obj.MOVEMENT.FORWARD then
  -- do something

elseif current_movement == obj.MOVEMENT.REVERES then
  -- throws error due to typo 'REVERES', so a silent mistake becomes a hard error:
  -- "'REVERES' is not a valid value (expected one of: 'FORWARD', 'REVERSE', 'LEFT', 'RIGHT')”

end



Hope that explains the use case.
Thijs