Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Lua Semicolon Conventions

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?

like image 743
MrHappyAsthma Avatar asked May 31 '13 17:05

MrHappyAsthma


People also ask

What does colon mean in Lua?

The :(colon) operator in Lua is used when you want to pass an invisible parameter to the method of an object that you are calling.

What does double dot mean in Lua?

Lua denotes the string concatenation operator by " .. " (two dots). If any of its operands is a number, Lua converts that number to a string.


1 Answers

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') 
like image 53
Mike Corcoran Avatar answered Sep 21 '22 03:09

Mike Corcoran