Simple question…
Can anyone explain the difference between ( ), { }, and [] when coding?
How to know which to use when?
Simple question…
Can anyone explain the difference between ( ), { }, and [] when coding?
How to know which to use when?
In short:
() – is for functions, for example: myFunction()
{} – is to for tables, for example: myTable = { itemOne, “Item two”, 57 }
[] – is to call objects from a table, for example myTable[3] would return 57
Correct. One minor addition is that [] can be used not only to call, but also to set elements in a table, for example:
myTable[4] = “Janice”
would either change the 4th element in a table to the string “Janice”, or add it as a fourth element if there wasn’t one already.
To add a little additional information…
Parenthesis () are used by functions, but they also are important for grouping items for math functions and in logic usage:
local a = 10 * 10 + 10
and
local a = 10 * (10 + 10)
are different results. It’s useful in if and while statements too.
Lua allows functions that are expecting tables to be passed in to skip the parens:
local tv = widget.newSuperDuperView{param=1, param2=“fred”}
instead of doing:
local tv = widget.newSuperDuperView( {param=1, param2=“fred”} )
both are allowed.
In short:
() – is for functions, for example: myFunction()
{} – is to for tables, for example: myTable = { itemOne, “Item two”, 57 }
[] – is to call objects from a table, for example myTable[3] would return 57
Correct. One minor addition is that [] can be used not only to call, but also to set elements in a table, for example:
myTable[4] = “Janice”
would either change the 4th element in a table to the string “Janice”, or add it as a fourth element if there wasn’t one already.
To add a little additional information…
Parenthesis () are used by functions, but they also are important for grouping items for math functions and in logic usage:
local a = 10 * 10 + 10
and
local a = 10 * (10 + 10)
are different results. It’s useful in if and while statements too.
Lua allows functions that are expecting tables to be passed in to skip the parens:
local tv = widget.newSuperDuperView{param=1, param2=“fred”}
instead of doing:
local tv = widget.newSuperDuperView( {param=1, param2=“fred”} )
both are allowed.
Thank you all. :wub:
Thank you all. :wub: