Idea: "Frozen" inner function

Michael Butscher mbutscher at gmx.de
Tue Nov 28 05:12:04 PST 2006


David Medlock wrote:

[What are closures?]
> In my understanding, closures are like functions which carry state along 
> with them.  Like objects but with only a single method: opCall().
> 
> in Lua syntax:
> 
> function make_adder()
>    local sum = 0;
>    return function(n) sum = sum + n; return sum; end
> end

What happens while the outer function is "alive" yet?

E.g. something like (I don't know Lua, therefore I guessed syntax):

function test()
   local sum = 0;
   local x = function(n) sum = sum + n; return sum; end
   sum = 5;
   print(x(5));
end


Does this print 5 or 10?



Michael



More information about the Digitalmars-d mailing list