lua-users home
lua-l archive

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




On Sunday, April 20, 2014, Marc Lepage <mlepage@antimeta.com> wrote:
I was wondering if there was a reason we cannot do syntax g (below) in a table. It seems like that would be handy.

t = {
    1, 2, 3,
    a='a',
    b='b',
    f=function() print 'f' end,
    function g() print 'g' end,
}

I'm sure there's a reason, I'm just curious.

Marc

Because the:

function x() end


Syntax is short for:

local x
x = function () end

Such that you can recursively call the x function by declaring the variable first. Without the prior declaration of x, a call to the function "x" within the function would result in a call to a global nil value. 

This shorthand won't work because x is a field inside your table, so declaring it in the local scope would be "broken."

-Andrew