Home Segments Index Top Previous Next

69: Mainline

When arithmetic expressions contain instances that belong to multiple classes, those expressions are called mixed expressions. The general rule about mixed expressions is that Smalltalk attempts to convert a number belonging to one class into a number belonging to another class in a way that does not lose information. Thus, when given a mixed expression that multiplies a Fraction instance by an Integer instance, Smalltalk creates a Fraction instance from the Integer instance in the course of producing an answer. Similarly, when given a mixed expression that multiplies a Float instance by an Integer instance, Smalltalk creates a Float instance from the Integer instance in the course of producing an answer:

6 * (2/5)         <-- Multiply (6/1) times (2/5), answering (12/5) 
6 * 3.1           <-- Multiply 6.0 times 3.1, answering 18.6