lua-users home
lua-l archive

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


I am currently writing a library that calls all sorts of external C/C++
functions: regexes, win32 stuff... Many of these functions use a slew of
numerical constants to describe their behaviour. Alas, defining such a
list of simple constants is not one of Lua's many strong points, if I
may say so.

I see three basic possibilities, all more or less ugly:

* trying to go down the string route. That won't work well as many of
the constants are to be or'ed together or will be used in (bitwise)
and's

* define a list of globals like this:
FA_READONLY=0x0001 FA_HIDDEN=0x0002 FA_SYSTEM=0x0004 FA_DIRECTORY=0x0010
FA_ARCHIVE=0x0020 FA_DEVICE=0x0040 FA_NORMAL=0x0080...

* define a table FA like this:
FA={READONLY=0x0001,HIDDEN=0x0002,SYSTEM=0x0004,DIRECTORY=0x0010,
ARCHIVE=0x0020,DEVICE=0x0040,NORMAL=0x0080,...}

I have a gut feeling that the third option is the best of the lot but I
would like to know what others have done when faced with long lists of
constants. (A quick search in the archives has brought no revelation.)

Things I consider are first and foremost ease of use (for the clients of
the library, not for me;-) and efficiency, space- and time-wise.

-- 
cheers  thomasl

web : http://thomaslauer.com/start