Design note: an operator keeps its meaning
This note goes with What are Operators in EK9 Operators. An operator is a shorthand, consistent way of defining a method on an Object, named by a symbol or a phrase. EK9 leans on operators heavily, and that only works if each one means the same thing on every type.
An operator is a method that reads naturally
result := a + b could be read as result := a.+(b), but the first form is much more natural and clearer (unless you like Lisp, in which case (+ a b) is clearer for you). The operator looks disconnected from the object, but it is still the object's method.
Addition does not modify
Everyone knows addition, written in mathematics with +. In 6 + 8 neither 6 nor 8 is modified. Some software APIs misuse a method called add to add something into a value, which changes the meaning of addition: Java List add is an example. The point is not to denigrate Java or any other language, but to show the value of operators that keep their meaning, in line with most people's long-term memory.
In EK9, adding something into a value is +=. + on a List gives a new List holding the existing contents plus the value, and leaves both operands as they were. The same holds for two Lists: the result holds the contents of both, and the two being added are unaltered.
Why it matters
- Generic code. This matters most when you write generic functions and classes: + must have the same meaning irrespective of type, or a generic function cannot rely on it.
- Reading. Most people know basic mathematics, so keeping the meaning of + lets a reader see immediately what code does. Being able to read code quickly and see what it accomplishes is valuable.
- No new names. Do not introduce misleading methods such as 'addTo', 'addInTo', 'addAll', 'join' or 'joinTogether' when + and += can be overloaded with different parameters and the same meaning.
The aim is to use a developer's long-term memory for fundamental concepts and their meaning, and to use symbols and ideograms, the visual side of the brain, to reduce cognitive load.
Focus
It is easy to be seduced by operators, and to override or overload them into contrived meanings. When you override an operator, take the time to make sure it keeps its meaning and stays consistent.
Remember too that a simple + may trigger a lot of processing. For two Lists it may create a new List and traverse both of the others to fill it, which is not obvious from the +. Or it may create a list that only refers to the two lists, giving the impression of joining them. That implementation detail is not what matters here; the meaning is.
Use them
Operators are made to be used and overridden. If you write an API for imaginary numbers and need addition, overriding + is not only acceptable, it is strongly encouraged. If you write a collection, using + and += to 'add' and 'add to' is strongly encouraged.
Use operators the way they were intended, and using your types with generics becomes much easier. From a C++ background this will feel natural - but take care not to misuse overriding and overloading.