lua-users home
lua-l archive

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


Hello fellow Lua users,

Over my experience reading e-mails on this mailing list, as well as
helping people on #lua, I've decided to write a library that implements
and enhances the functionality of require().
I have snippets of code from over the years, so the work will really
just be assembling them into something useful and coherent.  Here are
some proposed usages of this library:

  -- local drop-in replacement
  local require = require 'robs.require'

  require 'foo' -- works just like _G.require!

This example introduces nothing new; in fact, it *should* be possible to
just do _G.require = require 'robs.require'.  However, I wouldn't
recommend it.

  -- Loading modules with arguments

  require('foo.with.config', { ... }) -- the arguments are passed
directly to the chunk

I've seen this requested a few times, and I've also wished for it in my
own modules.

  -- Loading modules in a custom environment

 require:withfenv(custom_env)('foo') -- sets the environment for the
loaded chunk

  -- Loading modules in their own environment

  require:isolated('foo')

This may be nice for simple sandboxing, or imposing "good module
behavior" onto modules that don't have it.

  -- Loading modules relative to one another

  require:relative('bar') -- loads foo.bar

This is one of the most frequently asked questions I see regarding require.

The "extended" forms are also re-usable, if you prefer a different syntax:

  local require_relative   = require.relative
  local pythonic_require = require.isolated

  require_relative 'bar' -- loads foo.bar

Turn off loading of C modules for the given loader (of course, you can
also add your own):

  require.loaders.c_loader               = nil
  require.loaders.all_in_one_loader = nil

Or for a single invocation:

  require:withloaders('preload_loader', 'lua_loader')('foo')

Or for a special loader:

  local lua_only_loader = require:withloaders('preload_loader',
'lua_loader')('foo')

Or with custom loader functions:

  require:withloaders(loader_func_1, loader_func_2)('foo')

Of course, you can chain these:

  local pythonic_lua_only_require =
require.isolated:withloaders('preload_loader', 'lua_loader')

So, what are the community's thoughts on this library?  Is it a good
idea?  A terrible one?  Any suggestions on syntax, or further feature
enhancements?  I'll try to make it as extensible as can be.

The feedback I really want to get is regarding the name.  'robs.require'
is a terrible name, and I'm open to suggestions.  I just suck at naming
things. =)

Thanks,
Rob