Passing a table without parentheses in Lua gives an error

The following gives the error near’Person” grammatical error, even in the faq (http://www.luafaq.org/) its statistics: “So it is clever Use the fact that Lua will accept a single function parameter without parentheses, if the parameter is a string or a table”

function class(cls)
return cls
end

Person = {}

class Person

print(Person)

I am here What did you miss? If I change the class Person to the class “Person” it works, but faq statistics it should work for strings and tables.

< div class="answer"> When there is only one parameter, you can only omit the parentheses, and the parameter is a string literal or a table constructor.

In your example, syntactically speaking, you The class’foo’ or class () can be called, but the class Person cannot be called, because Person is a variable, not a table constructor.

The’Person’ is given below The error near the’syntax error, even in the faq (http://www.luafaq.org/) its statistics: “So it cleverly uses the fact that Lua will accept a single function parameter but no parentheses, if the parameter is String or table”

function class(cls)
return cls
end

Person = {}

class Person

print(Person)

What am I missing here? If I change the class Person to the class “Person” it works, but faq statistics should apply to strings and tables.

When there is only one parameter, Only the parentheses can be omitted, and the parameters are string literals or table constructors.

In your example, syntactically speaking, you can call class’foo’ or class (), but The class Person cannot be called because Person is a variable, not a table constructor.

Leave a Comment

Your email address will not be published.