Hammerspoon / hammerspoon

Staggeringly powerful macOS desktop automation with Lua
http://www.hammerspoon.org
MIT License
12.12k stars 584 forks source link

[Question] Compound Assignment Operators for Lua? #2417

Open asmagill opened 4 years ago

asmagill commented 4 years ago

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:

Cons:

Thoughts?

dasmurphy commented 4 years ago

If you implement it, add a flag to enable or disable the modifications in the preferences. This should be marked as 'experimental language extensions' or something. I don't know if this is good or not.

Are there any drawbacks for the users?

These are my current 2 cents even if i have not a big voice here, since i'm fairly new here, but i'm following the project.