lua-users home
lua-l archive

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


I am looking at cutting an existing c++ algorithm to lua.
The current c++ version uses a lot (~30) of #defined state flags.

my lua options seem to be
1/ just use integer constants
if s == 1 then ...... elseif s==2 then ... end
should be fastest, but hardest to read.

2/ use local vars
local STATE_1, STATE_2, STATE_3 = 1,2,3
if s == STATE_1 then ... end

this reads nicely, but all those locals are a pain, and i don't really need variables - the states are just symbols

3/ use strings
if s == "STATE_1" then ...... end

still reads nicely, but are all those string comparisons going to cost much ?

any other suggestions ?

i don't like the #define approach, but could lua do with a "const" keyword, for immutable data (symbolic constants)? this would presumably be translated to the literal value during compile.
eg
const STATE_1 = 1
if (s == STATE_1) then...

would compile to
if (s == 1) then...

feel free to tell me that is impossible / a bad idea because.. / doesn't match the LUA design philosophy / etc. i admit to liking LUA as small, simple and fast as possible, since it is so extensible anyway, but i do like symbolic constants, esp. for state-machine style algorithms.

Adrian