lua-users home
lua-l archive

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


Gang,

I know lua has no preprocessor, but is there a way to implement constants in
lua?

I'm setting up a table, currently with hard coded numbers:

t = {
    a = function() Dispatch(100) end,
    b = function() Dispatch(101) end,
    c = function() Dispatch(102) end
    }

(Dispatch is a registered C routine to do the internal dispatching in my
app.)

I'd like the flexibility of defining a base number (eg 100) and the rest be
offsets so that if I need to shift the entire range of numbers, I need only
change one.

For example

base = 100

t = {
    a = function() Dispatch(base) end,
    b = function() Dispatch(base + 1) end,
    c = function() Dispatch(base + 2) end
    }

I know I can do the above, but I don't want the extra bit of variable look
up and math on each call... I want "base + x" to be resolved at compile time
into a number constant.

Is this possible?  

If not, I may implement my own preprocessor to do what I need.  One question
about that:

If I replace instances of base with a constant number:

t = {
    a = function() Dispatch(100) end,
    b = function() Dispatch(100 + 1) end,
    c = function() Dispatch(100 + 2) end
    }

And then have lua compile that, is the compile process smart enough to
collapse the contant math into a single contant: such as

t = {
    a = function() Dispatch(100) end,
    b = function() Dispatch(101) end,
    c = function() Dispatch(102) end
    }

Or if I call b, will it perform 100 + 1 and than pass that to Dispatch?

Thx,
Ando


-----------------
SpriTec Software
www.spritec.com