lua-users home
lua-l archive

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


On Apr 11, 2013, at 2:50 PM, Roberto Ierusalimschy <roberto@inf.puc-rio.br> wrote:

> Anyway, can't you hanlde all them the same way, by packing each module
> inside a function, more or less like this?
> 
> do    -- module A "inlined" here
>  local _ENV = _ENV
>  local function mod (...)
>    <<module original source code>>
>  end
>  package.preload.A = mod
> end

Wow… the delicious irony… this just look exactly like all my 5.2 module code:


do --8<--

  local _ENV = module( 'IMAP' )

  -- stuff goes here...

end -->8--


E.g. http://alt.textdrive.com/svn/altdev/Mail/IMAP.lua


'module' itself being defined as:

local function module( aName, ... )
  local aModule = package.loaded[ assert( aName ) ]

  if type( aModule ) ~= 'table' then
    aModule = {}
    aModule._M = aModule
    aModule._NAME = aName
    aModule._PACKAGE = aName:sub( 1, aName:len() - ( aName:reverse():find( '.', 1, true ) or 0 ) )

    package.loaded[ aName ] = aModule

    for anIndex = 1, select( '#', ... ) do
      select( anIndex, ... )( aModule )
    end
  end

  return aModule
end


How caustic that we now all have to find new, wonderful, and half-baked ways to re-invent 5.1 module functionalities…  progress it is not.