Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Can someone explain when and why you use ; this sometimes??

Asked by 4 years ago
Edited 4 years ago

That's the whole question lol.

1
so you can write multiple statements on the same line like `print(5);print(3)` theking48989987 2147 — 4y
0
oh ,ok Thank You Freddan2006YT 88 — 4y
0
we can use it to write multiple statements, but a lot of the time it's just habit. people write in other coding languages which can carry over to Lua, leading to people ending declarations with the semicolon (but not function calls). Fifkee 2017 — 4y

1 answer

Log in to vote
1
Answered by 4 years ago

Semicolons are usually optional and can be used to visually separate statements (ex on the same line). They are mandatory if you do something like this:

local myVar = 5
print("Hello")
(myVar == 5 and print or warn)("This line will error because there is no semicolon")

Specifically, it will error with ambiguous syntax (function call x new statement) near '('. Adding a semicolon after line 2 or at the start of line 3 fixes it:

local myVar = 5
print("Hello")
;(myVar == 5 and print or warn)("This message will be printed since myVar is 5")

Does this happen in practice? It can; though it's rare, sometimes it's nice to be able to specify which function you want to use (via the and or "ternary") in the same line you use it.

Ad

Answer this question