Can anybody explain this syntax?

Was reading this code and I came across some Lua syntax I’ve never seen before. Can anybody explain?

1 Like

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.

6 Likes

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
5 Likes

btuh, everyone knows that fusion docs uses this type of code :person_facepalming:
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.

2 Likes

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!

2 Likes

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" 
1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.