The operator-overloading tag is defined as stack overflow as
a programming language function that allows you to configure implementations for operators depending on the types of operands used. Some languages โโallow you to define new operators, while others allow you to redefine existing ones.
In smalltalk
All types are defined as feature classes *
All operators are methods *
All methods are executed by the receiver of the message with the method name.
All methods may be overloaded.
Thus, any operator working on any operand can be overloaded by any developer.
Here are some examples:
Objects of the Float class, SmallInt class, Fraction class, and Point class can respond to a + message. They can also interact with each other.
aFloat := 3.1415 . aSmallInt := '6' . aPoint := 3@3 . aFraction := 22/7 .
"send a + aSmallInt to aFraction "
aSum := aFraction + aSmallInt Computes: 64/7
"send a + aFraction to aSmallInt "
aSum := aSmallInt + aFraction Evaluates: 64/7
aSum := aFloat + aFraction aSum := aFraction + aFloat They evaluate: 6.284357142857143
aSum := aFloat + aSmallInt aSum := aSmallInt + aFloat They evaluate: 9.1415
aSum := aPoint + aSmallInt aSum := aSmallInt + aPoint They are evaluated as follows: 9@9
In fact, we have 8 different implementations of the + operator displayed here, each of which is configured to work with the types of operands involved.
Disclaimer: * Objects are not very typed. Any variable of one type can be changed to any other type, and the system will not throw an exception. An object can start as an object of the SmallInt class, and then be replaced with a ByteString or Dictionary, and the system will not cause the slightest warning. Until a message is sent that it does not understand.
There are 6 primitives that are not an object or an object class: true, false, nil, etc.
There are two operators that are essentially syntactic sugar for these methods.