The examples we looked at in Chapter 3, such as
Complex (despite its name), are simple because they’re closed under all four arithmetic operations. Whenever
Complex objects, then
x+y, x-y, x*y, and
x/y are also
Complex objects (except for division by zero). We didn’t have to think about which arithmetic operators to implement and what their results should be.
Most numeric data in the real world, however, have some unit of measure. An amount of money may be measured in dollars, a distance in meters, and a temperature in degrees Kelvin. Such data are rarely closed under all ...