A Tour of Scala: Compound Types

Created by admin on 2008-07-05.
Updated: 2008-12-21, 01:02

Sometimes it is necessary to express that the type of an object is a subtype of several other types. In Scala this can be expressed with the help of compound types, which are intersections of object types.

The question arises what the type of the parameter obj is. If it's Cloneable then the object can be cloned, but not reset; if it's Resetable we can reset it, but there is no clone operation. To avoid type casts in such a situation, we can specify the type of obj to be both Cloneable and Resetable. This compound type is written like this in Scala:Cloneable with Resetable.