Can operators in Smalltalk be overloaded?

Can operators be overloaded in Smalltalk?

I am looking for tutorials / examples.

Thanks.

+6
source share
5 answers

Method overloading is not possible in Smalltalk. Instead, a combination of method overrides and a method called double dispatch is used to implement the same behavior as overloading statements in other languages.

You can find an example implementation in the mathematical operators +,*,/,- (which are binary messages in Smalltalk). Here's the idea: an Integer>>+ implementation sends the #addWithInteger: message to its argument. Implementation #addWithInteger: implemented on each Magnitude subclass, for example, to specialize the addition of Int + Int, Float + Int, etc.

+16
source

In most cases, things that are operators in other languages โ€‹โ€‹are in the same or binary Smalltalk messages, such as +, *, /, ... etc. Classes can respond to these messages as they see fit, so yes, you can override + behavior, and you can also understand how some classes of numbers do not understand and respond to it.

For example, look at the implementation of the + class in Point.

One note: = and ^ are not messages, so they cannot be overridden in the manner described above.

Btw, for teaching Smalltalk, one of the greatest resources for examples and code is the Smalltalk image. Therefore, I recommend that you start Smalltalk and find out your path to view the vast amount of examples that it contains.

+12
source

There are no operators in smalltalk other than assignment. Everything is implemented in classes as methods. Therefore, if you want to change the behavior of the methods = or +/-, just look at their developers. Or, if you want instances of your class to understand these messages, just follow them.

+6
source

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.

+1
source

Smalltalk has no operators, but you can achieve similar results by defining / overriding the method:

 Object subclass: Foo [ + anObject [ "define new behavior for + here" ] ] 
0
source

Source: https://habr.com/ru/post/891698/


All Articles