lua-users home
lua-l archive

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


If my reading is correct it would not matter if "globals" p1 and p2 changed
because a closure was formed at the time of the DoSomething call.
In fact I would think that the closure mechanism could be used to achieve
what you wish, but I'm far from an expert so check what I am suggesting (and
yes, just suggesting, not telling).

BTW: You're last example should not function correctly (of this much I'm
sure :)

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

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

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

Should result in f(p1, p2) being called and its return value (nil) passed to
DoSomething as a parameter.

However, you should be able to specify to your users that the Callback
receives no parameters and they can "overcome" this restriction by creating
a closure (this is the part where I'm not 100% sure of myself)

Example of user circumventing that "annoying" library provider who didn't
let him pass any arguments to his callback...
---------------
local x, y;
local p1, p2 = 10, 20

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

DoSomething(x, y, function return callback(p1, p2) end)
--------------


If this looks like too much for your users to comprehend then you could
offer the following rather bizarre helper function...

function BindParameters(f, ...)
	return function return f(unpack(arg)) end end
end

which could be used to make the DoSomething call from above look like....

DoSomething(x, y, BindParameters(callback, p1, p2))


-----Original Message-----
From: lua-bounces@bazar2.conectiva.com.br
[mailto:lua-bounces@bazar2.conectiva.com.br]On Behalf Of Ando Sonenblick
Sent: Friday, October 17, 2003 4: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