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?(object, type)
tests type membership.subtype?(type1, type2)
tests type inclusion.make(type …)
makes an instance. This operation is only supported if the type is instantiable.- 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 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.)
- 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.