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

will be true if and only if`x`, limited(<integer>, min:`y`, max:`z`))`instance?(`

,`x`, <integer>)`(`

, and`y`<=`x`)`(`

are all true.`x`<=`z`)`instance?(`

will be true if and only if`x`, limited(<integer>, min:`y`))`instance?(`

and`x`, <integer>)`(`

are both true.`y`<=`x`)`instance?(`

will be true if and only if`x`, limited(<integer>, max:`z`))`instance?(`

and`x`, <integer>)`(`

are both true.`x`<=`z`)`subtype?(limited(<integer>, min:`

will be true if and only if`w`, max:`x`),

limited(<integer>, min:`y`, max:`z`))`(`

and`w`>=`y`)`(`

are both true.`x`<=`z`)`subtype?(limited(<integer>, min:`

will be true if and only if`w``…`),

limited(<integer>, min:`y``…`))`(`

is true.`w`>=`y`)`subtype?(limited(<integer>,`

will be true if and only if`…`max:`x`),

limited(<integer>,`…`max:`z`))`(`

is true.`x`<=`z`)

## 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.