lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On 28/10/2019 17.22, Pavel wrote:
Why such an expression without assignment should result in syntax error? Even if it is meaningless for numbers, it could be a+b with metametatables that does something else regardless the return value.

An expression isn't a statement.

While the Lua interpreter turns expressions into statements by
implicitly prefixing them with `return ` (which results in the printing
of the result), this doesn't really make sense within a program/script.

You could turn it manually into a statement by saying

  _ = a + b

(and potentially declaring `local _` first somewhere above.)

-----

If you were to change the syntax of Lua to allow `a + b` as a statement
(which should then evaluate the expression and discard the result),
then you'd surely have to also allow `a-b`, `a*b`, … (i.e. all
arithmetic expressions), probably even all unary/binary operators.

And then what's the meaning of

  foo = a
  "b" .. c

?

Is it still `foo = a( "b" ) .. c`?  Is it `foo = a ; "b" .. c`?
Something else?

-- nobody

--
Syntactic sugar causes cancer of the semicolon.  -- Alan Jay Perlis