lua-users home
lua-l archive

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



On Wed, Jun 5, 2019 at 10:01 PM Philippe Verdy <verdy_p@wanadoo.fr> wrote:
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.

That's debatable, and it's also irrelevant. I'm not saying this as a direct "this is bad so it shouldn't be allowed" statement. I actually like assignments as expressions.

But what I described is the history of Lua and the rationale that went into it. Is there another way that it could have been addressed? Sure. But that's not the decision that was made.

It's also a debate that has no bearing on the fact that f(x=0) isn't allowed because assignment is a statement, not an _expression_. The ability to chain function calls has nothing to do with it.

/s/ Adam