lua-users home
lua-l archive

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


Here is roughly what we're doing on my project:

1. There is a registry of namespaces.

2. You get something out of the registry by executing:

    local ns = import "some_namespace_name"
        -- Note that names can be qualified a la "some_namespace.foobar.com"

3. You put something into the registry by executing:

    export "some_namespace_name" {
        test = function() print "Hello!" end
    }

4. If import can't find an already registered item for the requested
namespace, it searches through a list of standard places looking at chunks
of metadata for various loadable entities to find one that reports that it
can load the requested namespace. If it finds such an entity, it loads it
and calls it. The loaded entity is then responsible for calling export.
Cyclic imports -- i.e., importing something that's in the process of being
imported -- are disallowed.

A library can export more than one namespace and the global space doesn't
get polluted. It seems to be working pretty well so far.

An interesting extension to this might be to make import a bit smarter. In
particular, one could support something like (assuming "table"  were a
namespace):

    local _, insert, remove = import "table" { "insert", "remove" }

This would do lookups for the requested items in the namespace and would
signal errors if they weren't present.

Mark