lua-users home
lua-l archive

Re: syntactic sugar for imports and exports

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


On Oct 19, 2011, at 4:30 PM, Petite Abeille wrote:
> 
> On Oct 20, 2011, at 12:40 AM, Tony Finch wrote:
> 
>> The usual proposal to improve the first two is as follows, but I'm trying
>> to find a more general solution.
> 
> What about something along these lines:
> 
> local function import( aName, ... )
> local aModule = require( aName )
> local aFunction = debug.getinfo( 2, 'f' ).func
> local _, anEnvironement = debug.getupvalue( aFunction, 1 )
> 
> for anIndex = 1, select( '#', ... ) do
> local aName = select( anIndex, ... )
> anEnvironement[ aName ] = aModule[ aName ]
> end
> end
> 
> import( 'math', 'min', 'max' )
> 
> print( min )
> print( max )
> 
>> function: 0x10001af60
>> function: 0x10001aee0
That populates the environment but what one probably really wants are locals.
Mark

AltStyle によって変換されたページ (->オリジナル) /