This is a bit off topic, but I was looking through some open source code (ie; Fusion) and I saw bits and bobs of code that simply looked, well, strange- for lack of a better purpose. Quotation marks were omitted when calling functions.
My question is, would proper habit to do such a thing; as follows.
Example one, showcase
local function test(str) --// expected 'hi'
return function(...) --// expected table empty
end
end
test "hi" {
}
Example two, usage
local meta = {}
meta.__index = meta;
local function new(name: string)
return function(properties: table)
return setmetatable(properties, meta)
end
end
--// Take note of this part, when using the 'new' function, no parentheses.
local obj = new "testing" {
Variable = 'var';
}
print(obj)
Alternatively, you can call the new function as expected via parenthesis.
furthermore, most, if not all scripters, are used to call functions with parentheses, calling them in a different way would only cause confusion for the person who would be reading your code.
I think you should not call your functions without () unless you are using a library that works with that type of syntax.
Edit: As I said, it is a personal opinion, if anyone knows the difference between calling the functions in another way, I would be grateful if you explain why!
This isn’t Luau functionality, this is vanilla Lua functionality. There is nothing wrong with omitting parenthesis if it is your team’s code practice. It is less commonplace in Luau coders because their knowledge of Lua syntax is biased to Roblox.
Be aware of differences between Lua and Luau (ex: never write environment {setfenv(), getfenv()} … changes post Luau release), but also understand that Lua syntax is almost fully compatible to Luau.
local x = {}
x.__index = x
function x.new(t)
return setmetatable(t, x)
end
function x.writename(s)
x.__metatable = s
end
x.writename "hidden table" -- parenthesis omitted for arguments
print(getmetatable(x.new{})) -- parenthesis omitted for arguments
-- "hidden table"
Just do what you’re comfortable with, you will see no performance difference.
It’s entirely preference. Most people prefer to always include the parentheses especially since not all argument types are supported when they are omitted. This can lead to confusion when reading code that alternates between both uses. For example:
print 2 -- This will cause an error
Some libraries actually take advantage of this syntax (see Fusion) and document their code as such. In the end it’s up to you though and there’s no performance difference.
Only works when you call a function while passing a single value as an argument to that function (and only for certain value types), it’s a purely cosmetic choice, personally I prefer to use parentheses for all function calls as it maintains consistency throughout.