Question

I was wondering if there is a general convention for the usage of semicolons in Lua, and if so, where/why should I use them? I come from a programming background, so ending statements with a semicolon seems intuitively correct. However I was concerned as to why they are "optional" when its generally accepted that semicolons end statements in other programming languages. Perhaps there is some benefit?

For example: From the lua programming guide, these are all acceptable, equivalent, and syntactically accurate:

a = 1
b = a*2

a = 1;
b = a*2;

a = 1 ; b = a*2

a = 1   b = a*2    -- ugly, but valid

The author also mentions: Usually, I use semicolons only to separate two or more statements written in the same line, but this is just a convention.

Is this generally accepted by the Lua community, or is there another way that is preferred by most? Or is it as simple as my personal preference?

Was it helpful?

Solution

Semi-colons in Lua are generally only required when writing multiple statements on a line.

So for example:

local a,b=1,2; print(a+b)

Alternatively written as:

local a,b=1,2
print(a+b)

Off the top of my head, I can't remember any other time in Lua where I had to use a semi-colon.

Edit: looking in the lua 5.2 reference I see one other common place where you'd need to use semi-colons to avoid ambiguity - where you have a simple statement followed by a function call or parens to group a compound statement. here is the manual example located here:

--[[ Function calls and assignments can start with an open parenthesis. This 
possibility leads to an ambiguity in the Lua grammar. Consider the 
following fragment: ]]

a = b + c
(print or io.write)('done')

-- The grammar could see it in two ways:

a = b + c(print or io.write)('done')

a = b + c; (print or io.write)('done')

OTHER TIPS

in local variable and function definition. Here I compare two quite similar sample codes to illustrate my point of view.

local f;  f = function() function-body end

local f = function() function-body end

These two functions can return different results when the function-body section contains reference to variable "f".

Many programming languages (including Lua) that do not require semicolons have a convention to not use them, except for separating multiple statements on the same line.

Javascript is an important exception, which generally uses semicolons by convention.

Kotlin is also technically an exception. The Kotlin Documentation say not only not to use semicolons on non-batched statements, but also to

Omit semicolons whenever possible.

In local variable definitions, we get ambiguous results from time to time:

local a, b = string.find("hello world", "hello") --> a = nil, b = nil

while sometimes a and b are assigned the right values 7 and 11.

So I found no choice but to follow one of these two approaches:

  1. local a, b; a, b = string.find("hello world", "hello") --> a, b = 7, 11
  2. local a, b a, b = string.find("hello world", "hello") --> a, b = 7, 11

For having more than one thing on a line, for example:

c=5
a=1+c
print(a) -- 6

could be shortened to:

c=5; a=1+c; print(a) -- 6

also worth noting that if you're used to Javascript, or something like that, where you have to end a line in a semicolon, and you're especially used to writing that, then this means that you won't have to remove that semicolon, and trust me, i'm used to Javascript too, and I really, really forget that you don't need the semicolon, every time I write a new line!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top