lua-users home
lua-l archive

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


The "if(x=0){ ... }" is an "issue" only because the assignment uses the operator "=" which is easily confused with "==" for the equality test.

It was still possible to write the assignment with ":=" (like in Pascal) and still use "==" for the equality test and still not "=" (unlike Pascal), to avoid the second issue with "x=0;" which would have been an _expression_ statement testing x but not using the result.

The final solution was to use ":=" for the assignment, "=" for the equality test, but allow _expression_ statements only after a keyword marking the fact we don't need to store the result, such as "void := x=0;" (needlesss) or "void := f();"; in that case "if(x=0){...}" is correctly performing a test of equality, and "if(x:=0){...}" is an assignment (whose result is false for if so that the block after it is not executed).

That "issue" is not really one, it does not "affect" the langages using "=" and "==". Assignments in the middle of expressions are very useful, and saves lots of lines of code.


Le mer. 5 juin 2019 à 22:28, Coda Highland <chighland@gmail.com> a écrit :

On Wed, Jun 5, 2019 at 3:16 PM Philippe Verdy <verdy_p@wanadoo.fr> wrote:
As well, Lua does not support "f(x=0)" because of currification, and then chosed to sacrifice the possibility of performing assignments in the middle of expressions.

That's not why Lua doesn't support it. Lua doesn't support it because assignment is intentionally a statement, not an _expression_, and function parameters must be expressions. This was a design decision, not a parser simplification -- a way to prevent the "if (x = 0) { ... }" issue that affects C-like languages.

/s/ Adam