lua-users home
lua-l archive

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


Matthew:
On a related note, may I assume from what you said that now the standard
contract for a module can be (incompletely) summarized as in the following
lines?

1. when loaded and called, a module's chunk should return a table containing
its "exported" symbols
2. a module should not create any global
3. ? any more items in the contract?

(BTW should this contract be the same for both Lua and C modules?)


Without wanting to speak for Roberto, I don't think you can ask
questions like these.
I'm sorry if I gave the false impression of wanting to pressure people. If I expressed myself in such a way to give such an impression, please let me apologize myself. English is not my mother tongue and I hope I didn't convey my enthusiasm in learning Lua and my doubts in a way that could upset people.

Lua always aims to let you as the developer
decide on any "contracts" you want to impose. Decide what fits best
for your application, and use that. Still as you'll know if you've
been following the list, there are simple "best practices" (not
necessarily specific to Lua), one of which is to avoid globals if you
can help it - but that doesn't mean you can't or shouldn't if your
desired design is best implemented this way.

The module() (and luaL_register) function in 5.1 is bad precisely
because it doesn't allow for this flexibility - i.e. it creates
globals whether you want it to or not.

So now it is quite simple... what the module returns will be the
return value of require, what you return is up to you, whether you
create globals is up to you, whether you assign to _ENV or simply
create a new table is up to you, whether you add anything else to the
"contract" is also up to you. Lua does not dictate things like this.


Yes, I'm aware of the great degree of freedom Lua allows a developer. That probably is my main source of confusion sometimes. As I mentioned before, my background in Java and C++ (and very little experience in highly dynamic languages) is probably hampering me in getting the right mindset yet (the "big picture", I mean). I see I still need much learning the "spirit" of Lua.
Thanks.
-- Lorenzo