upvalues

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

upvalues

Luiz Carlos de Castro Silveira Filho
Hi

Why can't we use something like this in Lua ?

local a = 1
function b()
	local c = function()
		print(%%a)
	end
end

It is surely strange, but by the upvalues definition (the constant value of
a variable in the moment the function was defined (right??)) shouldn't
constructions like this be possible ?

... or function c only gets defined when we call b ?


couldn't lua handle this automatically ?:

local a = 1
function b()
local a = %a
	local c = function()
		print(%a)
	end
end

Reply | Threaded
Open this post in threaded view
|

Re: upvalues

Luiz Henrique de Figueiredo
>Why can't we use something like this in Lua ?
>
>local a = 1
>function b()
>	local c = function()
>		print(%%a)
>	end
>end
>
>It is surely strange, but by the upvalues definition (the constant value of
>a variable in the moment the function was defined (right??)) shouldn't
>constructions like this be possible ?
>
>... or function c only gets defined when we call b ?

Yes, that's the main point in understanding visibility and upvalues: c only
gets defined when b is run.

>couldn't lua handle this automatically ?:
>
>local a = 1
>function b()
>local a = %a
>	local c = function()
>		print(%a)
>	end
>end

Yes, it could. There has been some heated discussion about this in lua-l.
See the archives.
--lhf