Reputation: 81
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
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
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 forf({fields})
; that is, the argument list is a single new table. A call of the formf'string'
(orf"string"
orf[[string]])
is syntactic sugar forf('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