Are there differences between
local tab = {
"hello";
2;
}
and
local tab = {
"hello",
2
}
?
Are there differences between
local tab = {
"hello";
2;
}
and
local tab = {
"hello",
2
}
?
They act in the same way when dealing with Tables. They can be used interchangeably.
This is completely valid syntax:
local tab = {1; 2, 3; 5, 2, 1; 4; 3}
Commas are used to either separate keys in tables or to separate function arguments/parameters.
Semi Colons can be used either to separate keys in tables or to end a line of code. This allows us to write code like this:
local function newFunc(); print("This is a func"); end; newFunc();
You can actually avoid using semi-colons there, and it’s still allowed. You could delete all new-line characters from a script and it would likely still function.
There are situations where the context is unclear, so a semi-colon is actually necessary. Take this (not particularly useful) example:
local realPrint = print
local function print(...)
realPrint(...)
return print
end
print"hi""there"
(function(x, y) realPrint(x + y) end)(1, 2)
--[[ output:
hi
there
function 0x123456
1 2 ]]
Whoops! Looks like it calls print
’s return with the addition function, instead of calling the addition function with (1, 2)
like intended. Now add a semi-colon:
local realPrint = print
local function print(...)
realPrint(...)
return print
end
print"hi""there"; -- semi colon now
(function(x, y) realPrint(x + y) end)(1, 2)
--[[ output:
hi
there
3 ]]
… and it prints the result of addition as intended.