Question

Lua has a really nice no-parenthesis call syntax that coupled with function closures allow me to write the following

local tag = 1
function test(obj)
    return 
        function(str)
            return 
            function (tbl)
                tbl.objtag = tag
                tbl.objname = str
                return tbl
            end
        end

end
test (tag) "def"
{
}

test tag "def" --error
{
}

However, if I remove the parenthesis around (tag), it results in a compile error. So why does Lua allow no-parenthesis parameters (i.e. "def") and not no-parenthesis var (table in this case) parameters?

Was it helpful?

Solution

From Programming in Lua:

If the function has one single argument and this argument is either a literal string or a table constructor, then the parentheses are optional:

My understanding of your above situation is that tag is a local variable (which is neither a literal string nor a table constructor), so test(tag) always requires parentheses. You don't need parentheses around "def" because test(tag) returns a function which accepts a single string, and that function is immediately applied to "def".

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top