lua-users home
lua-l archive

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


On Jan 12, 2010, at 10:10 PM, Mark Hamburg wrote:

> Oh. And we then don't have the module available for us to return from require should we want it. So, we write:
> 
> 	local M, env = module( ... )
> 
> 	in env do
> 		-- construct module
> 	end
> 
> 	return M
> 
> But at that point, why not just make explicit reference to M everywhere we want to populate it?

I missed this because it was on a different thread. I rather like it though it doesn't, answer the problem of returning the environment and its implementation probably needs to use getfenv to get the current environment:

	in package.seeall( module( ... ) ) do
		-- construct module
	end

To deal with the return problem, maybe:

	in _, M = package.seeall( module( ... ) ) do
		-- construct module
	end

	return M

That syntax is sugar for:

	local <varlist> = <expr>

	in ( <varlist> ) do
		-- statements
	end

Still a bit subtle since common usage will need a no-op first parameter. Another option is:

	function prependLast( ... )
		return ( select( select( '#', ... ), ... ) ), ...
	end

	local _environment_, <varlist> = prependLast( <expr > )

	in _environment_ do
		-- statements
	end

This then enables, a whole series of things like:

	in M = module( ..., package.seeall ) do
		-- statements execute with seeall but the module isn't contaminated
	end

	return M

	in C = class( ... ) do
		-- construct class
	end

Note, however, that we're still restricted in our ability to run arbitrary utility code within the class construction. For example, we can't call out to code to add a bunch of fields or methods unless we pass it C or unless it's defined as a standard part of the class.

Mark