Limited operator overloading
Operator overloading goes against V's philosophy of simplicity and predictability. But since scientific and graphical applications are among V's domains, operator overloading is an important feature to have in order to improve readability:
a.add(b).add(c.mul(d))is a lot less readable than
a + b + c * d.
Operator overloading defines the behavior of certain binary operators for certain types.
Operator overloading is possible for the following binary operators:
+, -, *, /, %, <, ==.
Implicitly generated overloads
==is automatically generated by the compiler, but can be overridden.
>=are automatically generated when
<are defined. They cannot be explicitly overridden.
Assignment operators (
/=, etc) are automatically generated when the corresponding operators are defined and the operands are of the same type. They cannot be explicitly overridden.
To improve safety and maintainability, operator overloading is limited.
- When overriding
==, the return type must be strictly
- Both arguments must have the same type (just like with all operators in V).
- Arguments cannot be changed inside overloads.
- Calling other functions inside operator functions is not allowed (planned).