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?
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With