lua-users home
lua-l archive

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


How about this?

---CUT here----------------------------------------

function CallBackFactory(callback_func, callback_param_table)
   return 
      function ()
         return callback_func(unpack(callback_param_table))
      end
end

-----------------------------------------------------
function DoSomething(x,y, callback)
   print (x, y)
   local done = true

   if(done) then
      callback()
   end
end

function Callback(a, b)
   print("callback : ", a+b)
end

DoSomething(0, 1, CallBackFactory(Callback, {10, 20}))
DoSomething(0, 1, CallBackFactory(Callback, {40, -40}))
---CUT here----------------------------------------


-----Original Message-----
From: Ando Sonenblick [mailto:ando@spritec.com]
Sent: Friday, October 17, 2003 2:37 PM
To: Lua list
Subject: Re: callback implementation details..

AB,

thanks for the feedback.

now what if we have this:

local x, y;
local p1, p2 = 10, 20

callback = function(a, b) print(a + b) end

DoSomething(x, y, f(p1, p2))

-- is that possible?  (I'd try it but haven't yet gotten my code in
place to do so...)

also, in your implementation, if p1 and p2 were globals and the values
changed after the call to DoSomething and the callback, that might not
be what the caller of DoSomething desired...

ando


On Friday, October 17, 2003, at 02:02 PM, Bilyk, Alex wrote:

> local x, y
> local p1, p2 = 10, 20
> DoSomething(x, y, function  callback() print(p1+ p2) end);
>
> The callback will print your 30.
> AB
>
>
> -----Original Message-----
> From: Ando Sonenblick [mailto:ando@spritec.com]
> Sent: Friday, October 17, 2003 12:47 PM
> To: Lua list
> Subject: callback implementation details..
>
> Gang,
>
> I have a feeling my question here is rather, well, stupid, but lua has
> proven so amazingly flexible that I just may be surprised that what I'm
> asking isn't just stupid, but actually possible....  yet I doubt it...
>
> Anyway, I am about to implement callbacks...   so that a script can
> call a C function to do some background process and when done, have the
> C code execute a lua call back.  Here would be a standard way:
>
> local x, y, p1, p2;
> DoSomething(x, y, {function  callback(a, b) print(a+ b) end, p1, p2});
>
> Where DoSomething is the call to C to initiate the bg process, x and y
> are parameters for that process, and the table is the description of
> the callback:
>
> it has the function to execute and the (variable number) of parameters
> to pass to it.
>
> In this case, say p1 and p2 are 10 and 20 respectively, my C code
> creates a ref to the table, does the work, then using the ref calls the
> function callback, passing p1 and p2, which prints out 30.  I see no
> problems with implementing this.
>
> What I'd love (for simplification sake) would be to somehow already
> pass the parameters to the function so that I don't have to have a
> table, or do extra parsing/passing in my C code, etc.
>
> So, in this case, things would look like (hypothetically speaking):
>
> local x, y, p1, p2;
> DoSomething(x, y, function callback(a = p1, b = p2) print(a+ b) end);
>     -- a is preassigned to p1 and b is preassigned to p2
>
> so in this case (assuming 10 and 20 again for p1 and p2), my C code
> could simply just call callback and the parameter a is already 10 and p
> already 20, yielding the same print out of 30.
>
> Is anything like this possible?
>
> thx,
> ando
>
> -----------------------
> Ando Sonenblick
> SpriTec Software
> www.spritec.com
>
>
-----------------------
Ando Sonenblick
SpriTec Software
www.spritec.com