lua-users home
lua-l archive

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


all requires should be assigning to a local variable.

you either need the sublibs for the main one or the user requires them.

so either user does

local state = require "lib/state"
local event = require "lib/event"

or main lib does

local scxml = {}

scxml.state = require "lib/state"
scxml.event = require "lib/event"

return scxml

then user does

local scxml = require "scxml"

scxml.state.blah() etc









On Mon, Feb 18, 2013 at 2:43 PM, Gavin Kistner <phrogz@me.com> wrote:
On Feb 17, 2013, at 10:15 PM, Miles Bader <miles@gnu.org> wrote:

> Gavin Kistner <phrogz@me.com> writes:
>> * Adds only a single `SLAXML` key to the environment; there is no spam
>> of utility functions polluting the global namespace.
>
> It should not add _any_ keys to the global environment.
>
>> ## Usage
>>    require 'slaxml'
>
> local slaxml = require 'slaxml'

Thank you for the suggestion. I've updated the library to use this pattern.

This works fine for this project where I only have two files. How would others suggest enforcing the same pattern for a different project that has many files all augmenting the same table?

For example, in LXSC[1] I currently have 10+ files like so:

    lxsc.lua
    lib/event.lua
    lib/scxml.lua
    lib/state.lua
    lib/...etc...

and I use this to build the common object like so:

    # lxsc.lua
    LXSC = { VERSION="0.3.1" }
    require 'lib/state'
    require 'lib/scxml'
    require 'lib/event'
    ...etc.

    # lib/event.lua
    LXSC.Event = { ... }

    # lib/scxml.lua
    LXSC.SCXML = { ... }

    # lib/state.lua
    LXSC.State = { ... }

How might I modify this small multiple of files to work together in a way that doesn't spam the global namespace?

I can't do something like:

    # lxsc.lua
    local LXSC = { VERSION="0.3.1" }
    require 'lib/state'
    return LXSC

    # lib/state.lua
    local LXSC = require 'lxsc'
    LXSC.State = { ... }

…because that causes a loop in the loader. Should I instead do this?

    LXSC = { VERSION="0.3.1" } # Spam the global for now
    require 'lib/state'
    require 'lib/scxml'
    require 'lib/event'
    local LXSC = LXSC
    _G.LXSC = nil # remove the global spam
    return LXSC

Your experienced advice is requested :)


[1] https://github.com/Phrogz/LXSC