lua-users home
lua-l archive

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




On 31/05/07, Wesley Smith <wesley.hoke@gmail.com> wrote:
You're right.  I will just have to make a table and pass it in.


Why simply not making val an argument to gen1? Arguments behave just as a local variables, if I understand correctly (i.e. they become upvalues).

Thus the code would simply be:

function gen1(val)
   return function()
       print(val)
   end
end


function constructor(generator)
   local val = 100

   return function()

      local f1 = generator(val)

      f1()

   end
end

constructor(gen1)()
-> 100

Hope it helps.

m.i.

PS: (please don't read)
if you want to have fun,

function bind_before(f, v) return function(...) return f(v, unpack(arg)) end end
function bind_after(f, v) return function(...) return f(unpack(arg), v) end end
gen1 = bind_before(bind_before, print)
constructor = bind_after(bind_before, 100)

constructor(gen1)()()
->100

sorry for being nerdy, I could not resist.