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