lua-users home
lua-l archive

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


On 24/10/2012, at 7:01 AM, spir <denis.spir@gmail.com> wrote:

> Hello,
> 
> In most languages with function closures, the following piece of code would print 1 and 2:
> 
> inc = function (i)
>   return function (n) return n+i end
> end
> 
> i = 1
> inc1 = inc(i)
> print (inc1(0))
> 
> i = 2
> print (inc1(0))		--> 1 or 2 ?
> 
> The reason is that so-called upvalues usually not are values, but "up-vars"

I think if you rewrite your example it becomes clearer:

inc = function (i)
  return function (n) return n+i end
end

j = 1
inc1 = inc(j)
print (inc1(0))

j = 2
print (inc1(0))

I don't know how Lisp might work, but I wouldn't expect any connection between the inch's local parameter i and the top-level's local variable j here.