+=, *=, ++, - - operators

I suspect you will not be disappointed in our judgment.

22 Likes

I support this. It’s so annoying typing out var = var + integer, it’s unnecessarily long. Even if we just got += and -=, I’d be happy.

3 Likes

https://developer.roblox.com/en-us/resources/release-note/Release-Notes-for-435

BLESSINGS!

36 Likes

Hey @zeuxcg, could += (or ...=, which could be more relevant/clear to its purpose) ever possibly be used as a shorthand for table.insert

e.g.

local numbers = {0, 42, 1337}

numbers += 9001
-- numbers is now {0, 42, 1337, 9001}

-- Above is the equivilant of:
table.insert(numbers, 9001)

This is more apparent when using a larger table structure:

local playerData = {
    points = 233,
    rank = "Noob",
    inventory = {"foo", "bar"}
}

playerData.inventory += "baz"
-- equivilant to: table.insert(playerData.inventory, "baz")

As you can see, using += takes up far less space, and can (arguably) be cleaner.

This won’t work. Backwards compatibility. What if I use metatables for operator overloading? I think adding tables like python allows with their data structures would be nice but of course this can’t happen.

There is no backwards compatibility here because this is new functionality. Overloading would, much like the default behaviour, disable this.

Python should not be used as an example; it is a very controversial and questionable language from a syntax perspective.

There is backwards compatibility issues, if I use __add, __sub, etc. table + number does not make sense since it is a mathematical operation, and it doesn’t make sense what that should evaluate to.

I disagree

@zeuxcg what if we got != as an alias for ~=? The ~ looks far too similar to -. Using != would make it a little clearer.

~=
-=
3 Likes

If that’s the case, we could also get ! as an alias for not?

if !a then
end
1 Like

I don’t think that’s a good reason to change this, because you can’t actually mix these up - -= is only valid in statement context, and ~= is only valid in expression context.

The only reason to use != would be to be more consistent with other languages, but that’s a pretty small win for a feature that already exists. We’ll see though.

8 Likes

I know but I actually have to focus on that little - or ~. I should be able to instantly know what operator it is.

o_o

1 Like

Meanwhile, you can switch to a font that makes them more obvious like Calibri or Consolas.

3 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.