Learn Scala Programming
上QQ阅读APP看书,第一时间看更新

Type constraints

Type constraints are rules associated with a type. They define a subset of all types that, for example, a variable can have. A type constraint takes the form of lower bound (subtype relation) or upper bound (supertype relation). It is possible to define multiple constraints for a single type. In this case, a type must satisfy both of them. Constraints are defined using the symbols >: (lower, unhappy bound) and  <: (upper, happy bound), and the direction of the sign corresponds to the reversed direction of the arrow on the UML diagram, as shown in the following screenshot:

The type constraints are inclusive, which is why type B represents both the upper and lower bounds. Besides B in our type hierarchy, only A obeys the LOWER type constraint and only  obeys the UPPER constraint.