lua-users home
lua-l archive

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


Hi, list!

I want to bring my own pet feature to discussion.

First, some background.

I do not write my own native Lua modules in my project, but organize
each file as follows:

    local foo, bar, baz = import 'path/to/file.lua' { 'foo', 'bar', 'baz' }

    local alpha = function()
      foo()
      bar()
    end

    local beta = function()
      bar()
      baz()
    end

    return
    {
      alpha = alpha;
      beta = beta;
    }

That is, bring to local scope anything what is used by calling import
function (that is a fancy loadfile with cache; it returns requested
keys from the table, returned by imported file; also accepts a table
instead of filename for complex cases; no caching then).

Then define locally whatever content is in that file for (alpha, beta
functions here). Note all unauthorized access to globals is prohibited
in runtime.

Then export public content by returning a table.

This is nice and clean scheme, and I like it.

However there is one weak spot -- the import function. It is too easy
to mess things up and write arguments in wrong order:

    local foo, bar, baz = import 'path/to/file.lua' { 'bar', 'baz', 'foo' }

Above example is a key to a painful debug session (that actually
happened a couple of times).

I dream of following syntax to define all mentioned locals automatically:

   import 'path/to/file.lua' { 'bar', 'baz', 'foo' }

Add/remove braces, quotes etc. to your taste. Consider also Erlang's
"-import(Module,Functions)." as a valid example of this approach in
*another* language.

This is probably the first *real* reason which affects *my* interests
where I'd wish for macros in Lua. I understand of course that it
should be quite simple to implement with token filters / Metalua etc.

Alexander.