Chapter 5

Types and Classes

The Type Protocol

The type protocol comprises the following:

The following is an informal description of type relationships: The function subtype? defines a partial ordering of all types. Type t1 is a subtype of type t2 (i.e., subtype?(t1, t2) is true) if it is impossible to encounter an object that is an instance of t1 but not an instance of t2. It follows that every type is a subtype of itself. Two types t1 and t2 are said to be equivalent types if subtype?(t1, t2) and subtype?(t2, t1) are both true. t1 is said to be a proper subtype of t2 if t1 is a subtype of t2 and t2 is not a subtype of t1.

subtype? on classes is defined by inheritance. A class is a subtype of itself and of its general superclasses.

subtype? on singletons is defined by object type and identity. If x is an object and t is a type, subtype?(singleton(x), t) will be true only if instance?(x, t) is true.

subtype? rules for union types are given in Union Types on page 72. subtype? rules for limited integer types are given in Limited Integer Types on page 74. subtype? rules for limited collection types are given in Limited Collection Types on page 126.

<object> is the root of the type hierarchy. All objects are instances of <object>, and all types are subtypes of <object>.

A number of operations on types are described in Reflective Operations on Types on page 343.

Base Types and Pseudosubtypes

Every type has a base type. The base type for a class is the class itself. The base type of a singleton is the singleton itself. The base type of a union is the union of the base types of its component types. The base type of a limited type limited(C, ) is C.

The type t1 is a pseudosubtype of the type t2 if t1 is a subtype of the base type of t2 and t1 and t2 are not disjoint.

Note that t1 being a subtype of t2 implies that t1 is a pseudosubtype of t2, but t1 being a pseudosubtype of t2 does not imply that t1 is a subtype of t2. Note also that if t2 is not a limited type or some other nonstandard type, then pseudosubtype is the same as subtype.

Base types and pseudosubtypes are used in the rules for sealing, described in Chapter 9, Sealing.

Type Disjointness

Informally, two types are disjoint if there can be no object that is an instance of both types. Formally, the disjointness of types is specified by the following set of rules. (Some of these rules reference definitions given in Limited Integer Types on page 74, Element Types on page 124 and Limited Collection Types on page 126.)