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

How do I use the semicolon ; symbol?

Asked by 9 years ago

All I really want to know is how use ; in my scripting and also I want to know when and what for I should use it.

Here is some code I have been using it for without knowing what it does:

return setmetatable({},{
    __metatable = true;
    __index = {};
    __newindex = function()
        error("Can't Edit Table")
    end
})

Blog (Last Script)

  • If anyone can I will be very grateful
0
Edited title. BlueTaslem 18071 — 9y

1 answer

Log in to vote
5
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
9 years ago

Semicolons have two uses in Lua.

Semicolons in Table Constructors

First, there's the use you named here, which is described in 3.6 of PIL -- semicolons inside tables are exactly the same as commas. They separate different elements in the table.

It is recommended that you use commas for "softer" separations, e.g., in their example

 {x=10, y=45; "one", "two", "three"}

Since x and y are string-name keys and "one", "two", "three" are part of the list declaration, there's a semicolon in between to indicate the separation. This indication is just for the human reader; Lua doesn't care.


Semicolons to End Statements

The other use of the semicolon in Lua is to mark the end of statements. This is only rarely necessary.

Here is a fact: Lua completely ignores whitespace1 (spaces, tabs, newlines -- things that don't have ink when you print them). That means y = x + 1 y=x+1 and

y
=
x
+
1

are all exactly the same.

The above quoted thing is a complete statement, so you can put a semicolon at the end of it to mark that it's done: y = x + 1;

Because Lua ignores whitespace, Lua doesn't care where you put your new-lines. That means that there can be ambiguity, or at least unclear code. For instance:

print("Hi")
(Instance.new("Part", workspace)).Name = "Brick"

In this case, the parenthesis are unnecessary -- but they are the cause of a potential problem.

The above could be interpreted like this:

( print("Hi") )(Instance.new("Part", workspace)).Name = "Brick"

If that doesn't look familiar, think about it like this:

local func = print("Hi")
func(Instance.new("Part", workspace)).Name = "Brick"
-- EDIT: fixed typo in above

That wasn't what you intended though. If you use a semicolon (;) Lua will know the first line is separate from the second.

Why the Semicolon?

Other programming languages (notably C (and friends) and Java) require semicolons after every statement. The practice is borrowed from there.

Some programmers, to be consistent between different languages, will use semicolons in Lua simply because it's familiar. It might be said that it would be good to use them always to be consistent with the rare occassions they are necessary (as in the above quoted code).


  1. This isn't completely true. The errors that are discussed later can be triggered by code because of new lines (since you likely made a mistake). Also, newlines obviously affect strings and comments. 

0
Thanks! TheDarkOrganism 173 — 9y
Ad

Answer this question