Skip to content

[Question] Compound Assignment Operators for Lua? #2417

Description

@asmagill

It has always been an (admittedly minor) annoyance of mine that Lua doesn't support compound assignment operators (e.g. var += 1).

At http://lua-users.org/wiki/LuaPowerPatches, I came across the following (about a page down, in the Lua 5.4 patch section):

Compound Assignment Operators

An update to SvenOlsen's popular compound assignment operator patch, allowing statements like "object.counter += 2". In addition to 5.4 (beta) compatibility, this adds shift and bitwise operators (<<=, >>=, &=, |=, and ^=) to the previously implemented +=, -=, *=, and /=. The ++ increment operator isn't included because it suggests a matching --, which is already used for comments. (And "+= 1" is only two more characters.) Also, compound assignment on tuples isn't supported, because (IMO) it makes it too easy to write really confusing Lua code, with little gain.

I'd like to open adding this particular patch up for discussion.

Pros:

  • adds C-like compound assignment operators familiar to many programmers
  • reduces typing for simple arithmetic and assignments

Cons:

  • for the reason given in the description, still doesn't add ++ or --
  • modifies lua source so keeping up with 5.4 point releases may require re-applying the patch or manual integration
  • we'd no longer be running "stock Lua 5.4" as our underlying language

Thoughts?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions