Chapter 5
Types and Classes
Limited Types
Limited types are subtypes of classes constrained by additional criteria. Limited types are
created with the function limited
. limited(<integer>, min: 0, max:
255)
and limited(<array>, of: <single-float>)
are examples
of limited types that are useful both for error checking and for optimization of compiled
code.
Limited types are not classes.
Errata: In the published book,
a comma is missing between 0
and max:
.
Limited Type Constructor
Limited types are created with the generic function limited
. The
first argument to limited is a class. Depending on the class, additional keyword arguments
are allowed to specify the constraints of the limited type.
Not all classes support limited
; the methods for limited
are
documented individually beginning
on page 263.
Limited Integer Types
Limited integer types are subtypes of <integer>
containing
integers that fall within a specifed range. The range is specified by min:
and max:
keyword arguments to limited
.
For example:
// accepts integers between -1000 and 1000 inclusive. define method f (x :: limited(<integer>, min: -1000, max: 1000)) … end method f; //accepts all strictly positive integers. define method f (x :: limited(<integer>, min: 1)) … end method f;
Limited Integer Type Protocol
If w, x, y, and z are integers, the following equivalences hold:
instance?(x, limited(<integer>, min: y, max: z))
will be true if and only ifinstance?(x, <integer>)
,(y <= x)
, and(x <= z)
are all true.instance?(x, limited(<integer>, min: y))
will be true if and only ifinstance?(x, <integer>)
and(y <= x)
are both true.instance?(x, limited(<integer>, max: z))
will be true if and only ifinstance?(x, <integer>)
and(x <= z)
are both true.subtype?(limited(<integer>, min: w, max: x),
will be true if and only if
limited(<integer>, min: y, max: z))(w >= y)
and(x <= z)
are both true.subtype?(limited(<integer>, min: w …),
will be true if and only if
limited(<integer>, min: y …))(w >= y)
is true.subtype?(limited(<integer>, … max: x),
will be true if and only if
limited(<integer>, … max: z))(x <= z)
is true.
Limited Collection Types
Limited collection types are subtypes of <collection>
(and
of subclasses of <collection>
) that are constrained to be a specified
size and/or to contain elements of a specified type.
A complete description of limited collection types is given
in Limited Collection Types
on page
126 in Chapter
8, Collections.