3 ms·
x = fn (y) Insert a newline: x = fn (y) Syntax error. What happened? The newline has no semantic meaning.
by tedunangst 12y ago
x = fn (y)
Insert a newline:
x = fn
(y)
Syntax error. What happened? The newline has no semantic meaning.
- samatman 12y agoYour problem is not what you think! Quite.... print(foo) ("x"):len() This creates the same error. Deleting the newline gives you a new, different error. A semicolon give the correct behavior. It's the edge case that the Lua grammar can't resolve, and it does the sensible thing, by refusing to compile. Also note that it is a compile-time error that is described as an ambiguity. It isn't a silent thing that bites you later, as in Javascript, and the difference is huge. [added] I'm not sure what you mean re "x() or y()" the following compiles fine: function t() return true end function f() return false end if t() or f() then print "logic" end and does indeed print "logic".
- tedunangst 12y agoComplete example: function fn(y) return y + 1 end x = fn (1) print(x) Works. function fn(y) return y + 1 end x = fn (1) print(x) Doesn't work. The only difference is a newline; clearly newlines do matter. (And I specifically added the space to show that it's not simply whitespace that matters.) This is, of course, documented in the manual. For the original example, I meant that expressions are not valid statements. true or false On a line by itself does not compile. It's an expression, but not a statement. This is one of the things I like about Lua, that it prefers explicit over clever, although sometimes the difference between statements and last statements gets annoying. I'd like to do function fn() return nil -- more code, stubbed out for testing end But you can't have code after return. Instead it needs to be wrapped with if true.
- samatman 12y agoQuite so. I didn't consider this aspect in my first post because it's a syntax error, not an ambiguity; unlike Javascript, Lua won't resolve your ambiguity for you. I share your feeling about Lua being a bit more statement oriented than I'd like. Have you checked out Moonscript?