lua-users home
lua-l archive

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

on 11/11/04 8:30 AM, Roberto Ierusalimschy at 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:

        local a
        local a

But the following is legal:

        local a
            local a

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

The second declaration of foo does not fill in the forward declaration.
Errors ensue.