V: implement auto op overload for +=, etc when +, etc overloaded

Created on 24 Apr 2020  路  1Comment  路  Source: vlang/v

What do you want?
I propose += op should be automatically overloaded by compiler when + has been overloaded. The behavior of A += B should be automatically defined as A = A + B.
And the same should be applied to other arithmetic ops (-=, *=, /=, %=).

Why do you want this?

  1. Compatible with other rules in V. V places many restrictions on op overloading. Now V requires in op overloading both arguments must have the same type. Alex also said in #4538 that the returned type should also be the same as arguments. This makes this proposal possible - A + B always return A's type (also B's type).
  1. Useful. Overloading on += etc is commonly used in areas such as scientific computing.

  2. Saving time. This is obvious - use += directly.

  3. Safe and natural. A += B should do A = A + B. Imagine a piece of code defining behavior of += not like this, I believe other programmers will complain about it.

Feature Request

Most helpful comment

Yes I think this will be supported.

>All comments

Yes I think this will be supported.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

oleg-kachan picture oleg-kachan  路  3Comments

taojy123 picture taojy123  路  3Comments

arg2das picture arg2das  路  3Comments

radare picture radare  路  3Comments

cjmxp picture cjmxp  路  3Comments