Was reading this code and I came across some Lua syntax I’ve never seen before. Can anybody explain?
It was a feature of Lua 5.1 but stayed because of backward compatibility. It allows you to call functions without writing down the left and right parentheses.
This is called Syntactical Sugar. This basically means that you can use a simpler form of syntax for your code.
There are some examples of syntactic sugar in LuaU. Such as Compound Operators.
Value = Value + 5 -- Adds 5 to the Value variable.
Value += 5 -- Much more simplified version.
Or a hashtag to get the length of a table:
local Length = #Table
For functions, you can call a function without the need of parentheses. Just as long as there is only one parameter, and it can only be a string or table value.
Foo"A" -- Valid
Foo{"A", "B"} -- Valid
Foo1 -- Invalid
Foo"A", "B" -- Invalid
btuh, everyone knows that fusion docs uses this type of code
By the way, yeah that’s the same as:
New("BindableEvent")({})
That’s prob just uses fusion (free asset) to detect when you reach a checkpoint on an obby.
I’m assuming.
Ah, so its just some syntax sugar. New is just a function being called with some params to the right of it. Got it, thank you!
I see. But in the code I attached they appear to be using two parameters rather than just one, a string and a table.
local checkpointReached = New "BindableEvent" {}
Edit: I figured this out myself. This syntax lets you use multiple params if you chain functions.
local function sugar(text)
print(text)
return function(text2)
print(text2)
end
end
sugar "Hello" "World"
-- In this case, we're passing "Hello" into the sugar function,
-- which returns a second function that expects a paramater. In this case, we pass in "World"
This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.