lua-users home
lua-l archive

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

> Without wanting to speak for Roberto, I don't think you can ask
> questions like these. 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.

Note that a "standard contract" does not mean "the only
contract". Clearly we want to keep the flexibility that modules can do
whatever they want.  A "standard" contract sounds to me quite similar to
"best practices": what a module should do unless it has a clear motive
to do otherwise.

> 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.

Lua does not dictate things like this, but in the specific case of
modules it "recomends" its external behavior. From PiL2, chapter 15:

  Usually, Lua does not set policies. Instead, Lua provides mechanisms
  that are powerful enough for groups of developers to implement the
  policies that best suit them.  However, this approach does not work
  well for modules.  One of the main goals of a module system is to
  allow different groups to share code.  The lack of a common policy
  impedes this sharing.

  Starting in version 5.1, Lua defines a set of policies for modules
  and packages (a package being a collection of modules).

It goes on to define these policies:

  From the user point of view, a module is a library that can be loaded
  through require and that defines one single global name containing
  a table.  Everything that the module exports, such as functions and
  constants, it defines inside this table, which works as a namespace.
  A well-behaved module also arranges for require to return this table.

So, this was the old "standard" contract. The new one removes the part
about "defines one single global name containing a table."

But you are right that Lua does dictate anything about how to implement
this "recommended" behavior (calling 'module', using _ENV, etc.). That
is, there is a "standard contract", but not a standard way to implement
this standard contract. (And modules are free not to follow the
standard contract.)

-- Roberto