lua-users home
lua-l archive

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


Am 07.04.19 um 09:00 schröbte Marc Balmer:
Hi all

Hi!


I have a question of style.  It's about using constants in modules.

Let's say we have a fictious C library libfoo that defines a preprocessor symbol INFO with a value of e.g. 42.

Then I write a Lua module foo which exposes libfoo's functionality to Lua.  Should INFO be an integer constant in foo or rather a string that is looked up using luaL_checkoption()?

i.e.

foo.somefunc(foo.INFO, 'test data')

or

foo.somefunc('info', 'test data')

It's probably only a question of style or taste, but what is your opininion (or remarks) on this?

That depends on how INFO is used. If it is passed unmodified to functions, I use string options. If it can be combined with other constants using bitwise operations, I use full userdata with interning for extra type safety. If you are supposed to pass an integer and INFO is just one particular example of such an argument, I use integers. The last one hasn't happened for me yet, I think.


- mb

Philipp