lua-users home
lua-l archive

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


It was thus said that the Great Soni L. once stated:
> (Hopefully it sends correctly this time)
> 
> We currently have raw requires, which means we do things like
> 
> local a = require"io.github.soniex2.a"
> local b = require"io.github.soniex2.b"
> local c = require"io.github.soniex2.b.c"
> -- use a, b and c normally
> 
> Instead of that, is there a module or something that lets me do 
> something like
> 
> local soniex2 = reqpath"io.github.soniex2"
> local a = soniex2("a") -- alternatively, `soniex2.a()`
> local b = soniex2("b") -- alternatively, `soniex2.b()`
> local c = soniex2("b.c") -- alternatively, `soniex2.b.c()`, with the call at the
>                          -- end for disambiguating between module and  path, and
>                          -- setting an __index is not an option as the module
>                          -- could already have its own __index (or not be a
>                          -- table at all; some modules return functions!).
> -- use a, b and c normally
> 
> I think it looks nicer.

  First off, why is this:

	local a = require "io.github.soniex2.a"
	local b = require "io.github.soniex2.b"
	local c = require "io.github.soniex2.b.c"

bad?  I see it as good because you explicitely list what you are including. 
If it's the typing, then something like:

	local M = "io.github.soninex2"
	local a = require(M .. ".a")
	local b = require(M .. ".b")
	local c = require(M .. ".b.c")

appears to work just as well, and is less typing that what you presented.

  Alternatively, you could play around with adding a function to
package.serchers[] (or package.loaders[] in Lua 5.1), although that might
lead to conflicts with module and submodule names.

  -spc