lua-users home
lua-l archive

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

On Tue, Feb 19, 2013 at 8:12 AM, Gavin Kistner <> wrote:
> Understood. I have benchmarking in place that shows no measurable performance degradation for my usage, though I do cache certain library functions in locals anyhow already.

Excellent! No need to be paranoid unless necessary...

> However, unless I'm missing something, this only works with strict (inverted) hierarchies of dependencies. It falls down if you two files that both extend the same base file and the user should be able to get code from both.

OK, let's look at a slight modification of your scenario:

# lxsc.lua
    local LXSC = require 'lib.module'
    require 'lib.state'
    require 'lib.scxml'
    require 'lib.event'

    return LXSC

    # lib/module.lua
    return { VERSION="0.3.1" }

    # lib/event.lua
    local LXSC = require 'lib.module'
    local Event = { ... }
    LXSC.Event = Event
    return Event

    # lib/scxml.lua
    local LXSC = require 'lib.module'
    local SCXML = {...}
    return SCXML

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

And no globals were harmed in the process, at the slight cost of an
extra module which just has the job of initializing the main table.
Thereafter require() ensures that it is unique.  All modules return an
appropriate table, which is strongly recommended.

steve d.