3 ms·
+= has a confusing interpretation when the lvalue is a metatable invocation or function result. For readability given the flexibility of Lua's operator overload
by scythe 4y ago
+= has a confusing interpretation when the lvalue is a metatable invocation or function result. For readability given the flexibility of Lua's operator overloading, it is always better to be explicit.
The ++ increment operator is even worse, because it leads to statements within statements. Even an experienced C programmer might have trouble understanding what happens when I write `y = x + x++;`. Lua's grammar achieves a simple parse with no statement delimiters because it enforces a distinction between expressions and statements.
- edflsafoiewq 4y agoWhat's confusing about it?
- neutrono 4y agoisn't y = x + x++; undefined behavior in C, because of a lack of a sequence point? If that's true, then it makes sense for no C programmer to know what happens.. I think to fix it you would have to go like: y = x; y = y + x++; I think that would work..
- bawolff 4y agoAll these problems could be solved by just having ++ and += not return a value, which gets rid of all the confusing cases but keeps the useful cases.
- UncleEntity 4y ago++ is useful to switch over the items of a string: switch (x++) { case ‘+’: if (x == ‘+’) { op = OP_INC; x++; } else if (x == ‘=‘) { op = OP_ADDEQ; x++; } else { op = OP_ADD; } break; … }
- wruza 4y agoIt may have a confusing behavior, not interpretation. += may mean “evaluate lvalue, add a value to it as usual and set it back”. If you end up getting confused by: t.x = t.x + v Then it is a question to t’s metatable designer either way.