Piece of Lua syntax that I don't understand

I'm using a Lua-based product, I'm using their API and there is a bit of syntax in there that I don't understand.

What is this? Is it a function call for Add, and if so what are the input parameters - there's nothing assigning that table to the variable input - no equals sign?

Is it a function definition for Add - that would seem odd, without any implementation and specifying what goes into the input tables?

Is Add a table containing tables? I've never seen a table created with parentheses instead of curly braces?

serviceDefinitions.Add(
    input { name="p1", baseType="NUMBER", description="The first addend of the 
    operation" },
    input { name="p2", baseType="NUMBER", description="The second addend of the operation" },
    output { baseType="NUMBER", description="The sum of the two parameters" },
    description { "Add two numbers" }
)

Upvotes: 5

Views: 990

Answers (2)

hjpotter92
hjpotter92

Reputation: 80639

It would have helped if you provided a link to documentation (or even the name of application), however:

serviceDefinitions.Add(
    input {
        name="p1",
        baseType="NUMBER",
        description="The first addend of the operation"
    },
    input {
        name="p2",
        baseType="NUMBER",
        description="The second addend of the operation"
    },
    output {
        baseType="NUMBER",
        description="The sum of the two parameters"
    },
    description {
        "Add two numbers"
    }
)

basically calls the Add function of serviceDefinitions table/class/struct/object with 4 parameters passed to it. Since, in lua, a function can be called without enclosing its arguments inside parenthesis in certain cases, the input, output and description functions are being called with a table as their parameter.

The results of each of these calls is then used as parameter to Add function.

Upvotes: 3

Adam
Adam

Reputation: 3103

When calling functions where there is only a single argument that is a table or string you can omit the parenthesis. From the manual:

All argument expressions are evaluated before the call. A call of the form f{fields} is syntactic sugar for f({fields}); that is, the argument list is a single new table. A call of the form f'string' (or f"string" or f[[string]]) is syntactic sugar for f('string'); that is, the argument list is a single literal string.

Which means that the following functions calls are valid:

somefunction({1,2,3,4})
somefunction{1,2,3,4}

Or, with strings:

print('hello!')
print 'hello!'

Upvotes: 7

Related Questions