3 ms·
I'm not sure Lua is that different than Javascript here. What is a semicolon in Javascript if not a character you can only place at the end of a statement? Ther
by tedunangst 12y ago
I'm not sure Lua is that different than Javascript here. What is a semicolon in Javascript if not a character you can only place at the end of a statement? There are some similar ambiguities in the Lua parser if you insert a newline into the middle of a statement.
The difference is that Lua is quite a bit fussier about statements versus expressions, and won't allow things like "x() or y()" as statements. So the Javascript example, translated to Lua, wouldn't compile with or without a semicolon.
- karma_fountain 12y agoYou can also place a ';' in the for loop, eg for(;;){ }
- tedunangst 12y agoThose are arguably equivalent to statements, but fair point. Not sure it matters in the Lua vs JavaScript comparison.
- samatman 12y agoThere are no such ambiguities. Here is the complete Lua grammar. You may confirm for yourself that semicolons are always optional and that newlines have no semantic meaning. http://www.lua.org/manual/5.1/manual.html#8 http://www.lua.org/manual/5.1/manual.html#8
- tedunangst 12y agox = 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?