lua-users home
lua-l archive

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


2010/1/12 David Kolf <kolf@gmx.de>:
> A.S. Bradbury wrote:
>>
>>> But in order to address the "reuse"-issue again, with loadin() it would
>>> only be easy to reuse the source and compile the function each time again.
>>
>> I may be missing something, but does this solve the multiple reuse problem?
>>
>> function multienv(funcstr)
>>   return loadstring([[
>>     return function(_G)
>>       in _G do
>>         return function()
>>           ]]..funcstr..[[
>>         end
>>       end
>>     end
>>     ]]
>>   )()
>> end
>
> No, that would operate on compiling source code for each environment.
> That is already possible with the new loadin () function. What I was
> looking for was reusing the function chunk without the need to compile
> it again each time and without the need to copy it in extra strings
> using string.dump ().

It doesn't compile for each environment. It compiles the function
once, and allows you to pass in an environment on subsequent
invocations. loadstring is called when you call multienv. You then
have a compiled function you can pass different environments to. The
code snippet in Roberto's post is much better though, and as lhf
suggests load can be used to avoid the string concatenation.

Alex