[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Redefining locals
- From: Mark Hamburg <mhamburg@...>
- Date: Thu, 11 Nov 2004 08:54:57 -0800
on 11/11/04 8:30 AM, Roberto Ierusalimschy at roberto@inf.puc-rio.br wrote:
> They capture the variables, not their values. The foo in that function
> is not free at all, it is forever binded to the local variable created
> with the first "local" declaration.
Which leaves the question that started this thread: Is it useful to be able
to capture a variable and then create a new variable with the same name in
what appears to be the same scope? (Arguably each local declaration creates
a new invisible scope that extends to the end of the visible scope.)
I can see a variety of levels of prohibition:
1. None -- i.e., what Lua has now. Creating a new local is always allowed.
2. Prohibit creating locals in the same block with the same name. Thus, the
following is illegal:
do
local a
local a
end
But the following is legal:
do
local a
do
local a
end
end
3. Prohibit creating locals that hide any existing locals. This rules out
the second example above.
In C, I've sometimes wished for 3, since the errors can be subtle, but I can
go either way on whether it's allowed or disallowed.
It's the second case that has bitten me a few times when I've written:
local foo -- forward declaration
local function baz() foo() end
local function foo()
if bar() then baz() end
end
The second declaration of foo does not fill in the forward declaration.
Errors ensue.
Mark