Chapter 5

Types and Classes

# The Type Protocol

The type protocol comprises the following:

- All types may be used as specializers for method parameters, bindings, and slots.
`instance?(`

tests type membership.`object`,`type`)`subtype?(`

tests type inclusion.`type1`,`type2`)`make(`

makes an instance. This operation is only supported if the type is instantiable.`type``…`)- Type objects are immutable.
- If two type objects are equivalent and are not classes, it is
unspecified whether they are
`==`

.

The following is an informal description of type relationships:
The function `subtype?`

defines a partial ordering of all
types. Type `t_{1}` is a subtype of
type

`t`(i.e.,

_{2}`subtype?(``t`_{1}, `t`_{2})

is true) if it is impossible to encounter an object that is an instance
of `t`but not an instance of

_{1}`t`. It follows that every type is a subtype of itself. Two types

_{2}`t`and

_{1}`t`are said to be equivalent types if

_{2}`subtype?(``t`_{1}, `t`_{2})

and `subtype?(``t`_{2}, `t`_{1})

are both true. `t`is said to be a proper subtype of

_{1}`t`if

_{2}`t`is a subtype of

_{1}`t`and

_{2}`t`is not a subtype of

_{2}`t`.

_{1}`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(`

will be true only
if `x`), `t`)`instance?(`

is true.`x`, `t`)

`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(`

is `C`, `…`)`C`.

The type `t_{1}` is a pseudosubtype of the
type

`t`if

_{2}`t`is a subtype of the base type of

_{1}`t`and

_{2}`t`and

_{1}`t`are not disjoint.

_{2}Note that` t_{1}` being a subtype
of

`t`implies that

_{2}`t`is a pseudosubtype of

_{1}`t`, but

_{2}`t`being a pseudosubtype of

_{1}`t`does not imply that

_{2}`t`is a subtype of

_{1}`t`. Note also that if

_{2}`t`is not a limited type or some other nonstandard type, then pseudosubtype is the same as subtype.

_{2}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.)

- Two classes are disjoint if they have no common subclasses.
- A union type is disjoint from another type if all of the union type's component types are disjoint from that other type.
- A singleton type is disjoint from another type if the singleton's object is not an instance of that other type.
- A limited collection type is disjoint from a class if their base
types are disjoint, or the class is a subclass of
`<collection>`

and its element type is definite and not equivalent to the limited collection type's element type, or the class is a subclass of`<collection>`

and its element type is indefinite and not a supertype of the limited collection type's element type. - A limited collection type is disjoint from a limited integer type because the
classes
`<collection>`

and`<integer>`

are disjoint. - Two limited collection types are disjoint if their base types are disjoint, or their
element types are not equivalent, or their sizes are not compatible. Two sizes are
compatible if either is
`#f`

, or they are`=`

to each other, or one is a sequence of integers and the other is the product of those integers. - Two limited integer types are disjoint if the minimum value of one is greater than the maximum value for the other.
- A limited integer type is disjoint from a class if their base types are disjoint or the
class is a subclass of
`<integer>`

whose range is disjoint from the limited integer type's range.