5

I have a condition where in my lua script I want to use enum like for SUCCESS I can give 1 and for FAILURE I can give 0 I am using lua version 5.2.4 Can anyone please help me on how to use enum I want to use enum

elseif(cc_config_cmd == "DELETE" and file_found==1)then
            api:executeString("callcenter_config queue unload " .. queue_name)
            stream:write("1")
else
            stream:write("0")

end
Egor Skriptunoff
  • 23,359
  • 2
  • 34
  • 64
ayush jain
  • 71
  • 1
  • 1
  • 5

2 Answers2

16

There are no enums in Lua.

Simply define variables.

SUCCESS = "1"
FAILURE = "0"

stream:write(SUCCESS)

Or put it into a table which would be quite similar to enum style syntax.

Result = {SUCCESS = "1", FAILURE = "0"}
stream:write(Result.SUCCESS)
Piglet
  • 27,501
  • 3
  • 20
  • 43
2

As far as I know, there is no enums in Lua, you can use strings such as your current code. The strings will be interned inside the Lua Virtual Machine, so in the memory the strings will not be duplicated.

Another option will be to use numbers in place of strings.

local COMMAND_DELETE = 1
local COMMAND_TEST_1 = 2
local COMMAND_TEST_2 = 3

Other options would be to use a third-party package such as the enum package or maybe go further and use a Lua Preprocessor

Robert
  • 2,711
  • 7
  • 15