2 ms·
Complete example: function fn(y) return y + 1 end x = fn (1) print(x) Works. function fn(y) return y + 1 end x =
by tedunangst 12y ago
Complete 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?