On Wed, Aug 14, 2019 at 9:32 PM Oliver wrote:
I would
prefer simple +=. I know from time to time this comes up in this list but I
never understood why Lua does not have +=.
Problem #1
Should new metamethods be introduced?
PRO: they would be useful for DSLs
CON: too many metamethods to implement in regular user classes.
Why methamethods ? Just define them as syntaxic sugars that make them equivalent to a standard assignment (including its existing methamethod) combined with the binary operator (including its existing methamethod). Adding new metamethods for them would confuse even more the task of programmers with unexpected behaviors if such new methemethod is implemtend but not coherently with the two other metamethods.
Problem #2
I guess XOR is the second popular operator (after addition) used in such short notation.
But we can't introduce short notation for XOR "a = a ~ b", because "~=" is already used for non-equality.
"^=" in Lua would be raising to the power, not XOR
For that we need another syntax like "~|=" (initially parsed as unary "~" but before the binary assignment-operator "|=" that it modifies).
Other useful sugars: "and=", "or=". The second one is useful to replace the common situation: x = x or 'default value'; which could be written: x or= 'default value';
Once again, don't introduce them with metamethods: the compiler will internally use a hidden (intrinsic) local variable for the reference to the left-hand "variable" (which may be an _expression_ returning a table-member reference, possibly a member of a table/object returned by a function call), and then will use the standard assignment (without any shortcut for "or=" or "and=": the assignment must be performed in all cases, even if the right-hand side is not always evaluated if the left-hand variable is already true (i.e. not false and not nil), causing the assignment metamethod being still called (even if the value assigned is the same as the existing one if the variable is already not false and not nil).
I don't favor adding new metamethods for all them (let's not reproduce the nightmare of C++ operator overrides that forces programmers to define both the operator and the operator-assignment and trying to "play" with complex accessors whose inheritance is hard to track). In summary, only the compiler will be updated, but compiled code at run time and metatables will not be modified with additional members.
Why only right addition/multiplication/division/etc?
"a *= b" means "a = a * b", but how to write "a = b * a"?
This might be important, for example, for matrices.
I agree: if one adds such operator-assignment syntax, it must be added for all operators.
Another useful sugar: not=x; equivalent to x=not x (using the unary "not" operator): in that case add not only "not=", but also "!=" (binary complementation, different from "not=" which is the logical operator testing for nil or false values), however, "!= x;" could still be written as "x ^|= -1" (using binaryxor-assignment) with the same effect as "x = x ~ -1" (except that the reference to "x" is computed only once if "x" is a member of table returned from complex subexpression).