Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to break a long line in Lua

Tags:

lua

Util = {
  scale = function (x1, x2, x3, y1, y3) return (y1) + ( (y2) -  (y1)) * \
        ( (x2) -  (x1)) / ( (x3) -  (x1)) end

}

print(Util.scale(1, 2, 3, 1, 3))

What is the proper syntax for breaking a long line in Lua?

like image 284
chrme Avatar asked May 19 '18 06:05

chrme


People also ask

Does Lua have break?

Lua, like most lanuages of this kind, has a "break" command that jumps out of the smallest enclosing loop. Normally you use "break" with "if" to decide when to exit the loop.

What is break in Lua?

In Lua, break statement enables us to come out of the inner block of a code. Break statement is used to end the loop. The break statement breaks the for, while, or repeat loop which includes the break statement. After the break loop, the code runs after the break statement and the broken loop.

How do you break a line in a python script?

To break a line in Python, use the parentheses or explicit backslash(/). Using parentheses, you can write over multiple lines. The preferred way of wrapping long lines is using Python's implied line continuation inside parentheses, brackets, and braces.

How do you comment on Lua?

A comment in Lua is text that is completely ignored by the parser. Comments are generally added by the script writer to explain the code's intent. A short comment starts with a double hyphen ( -- ) anywhere outside a string and it extends to the end of the line.


1 Answers

In your particular case, the convention would be ...

Util = {
   scale = function (x1, x2, x3, y1, y3) 
       return (y1) + ( (y2) -  (y1)) * ( (x2) -  (x1)) / ( (x3) -  (x1)) 
   end

}

Where the break is on statements Further breaking could be done if the multiplication needs to be split with

Util = {
   scale = function (x1, x2, x3, y1, y3) 
       return (y1) + ( (y2) -  (y1)) * 
               ( (x2) -  (x1)) / ( (x3) -  (x1)) 
   end

}

With the multiplication token used to split the line. By leaving a token at the end of the line, the parser requires more input to complete the expression, so looks onto the next line.

Lua is generally blind to line characters, they are just white-space. However, there are cases where there can be differences, and I would limit line breaks to places where there is an obvious need for extra data.

 a = f
 (g).x(a)

Is a specific case where it could be treated as a = f(g).x(a) or a = f and (g).x(a)

By breaking after a token which requires continuation, you ensure you are working with the parser.

like image 91
mksteve Avatar answered Oct 12 '22 19:10

mksteve