lua-users home
lua-l archive

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


2010/12/2 Miles Bader <miles@gnu.org>
> - The fact that you have to declare locals before using them.

Er, sure...

I'm a bit confused as to how somebody can be tripped up by this though
-- if they have the concept of a "local variable" at all, then surely
they're acquainted with the notion of declaring them, as that's the
only way to make a local variable in 99% of programming languages
(ignoring function parameters, but presumably they're "special")...

In _javascript_, function declarations are hoisted, for example. IIRC, in Ruby, you can define the methods of a class in any order you like, and I think that the same goes for attributes. In Ruby and Python, variables are local by default, and I don't think that the order in which they are defined matters.

I remember trying this, expecting it to work:

    local function foo() return bar() end
    local function bar() end

    assert(foo()) --> false

a variation of this is this other noob error (I did it too):

    local tbl = {
        foo = function(arg) tbl.field = arg end
    }

In Python the following works:

    def foo():
        def bar ():
            zog()
        def zog():
            print "Zog!"
        bar()

    foo() # prints "Zog!"
    bar() # error ( bar is a local inside foo()).



-- Pierre-Yves