Crazy Idea: How about compound assignment for infix operators?

So I ended up abusing some operators by overloading them for things that were not really arithmetic. I made them private and the definition and only uses of these operators were in a space of 15 lines of code, so the abuse was very local…

Ideally, I would use infix operators rather than misusing the arithmetic operators. However, the arithmetic operations have a huge advantage over infix operators in that they can be used to do compound assignment. So instead of saying something like

someLongName = someLongName + aValue

I can of course say

someLongName += aValue

Which you cannot do with infix functions, but the thought occurred to me, why not? Why doesn’t the language let you create compound assignment with infix functions? If I had an infix function called foo, why couldn’t the language let me replace:

someLongName = someLongName foo aValue

with

someLongName foo= aValue

in the same way that it builds += from a plus method, why not foo= from a foo method?

(If you are curious I used += to append and %= to prepend for composing functions)

1 Like

This looks possible to implement, not sure about the number of use cases, though

I like the idea.

It makes infix functions behave like operators, which makes sense to me.

Also, I find the syntax someLongName foo= aValue instantly understandable and more readable than someLongName = someLongName foo aValue :slight_smile: