chrme
chrme

Reputation: 113

How to break a long line in 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?

Upvotes: 10

Views: 17370

Answers (1)

mksteve
mksteve

Reputation: 13073

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.

Upvotes: 8

Related Questions