(rontolisp) docs
← Macros

typep

(typep object 'type-specifier &optional environment)

Tests whether object is of the given type. Lite: the type specifier is normally a literal (quoted) type — the same set typecase supports (atomic names, registered classes, zero-parameter user deftype names, and the compound specifiers (or ...)/(and ...)/(not ...)/(member ...)/(eql ...)/(satisfies ...)/ranged numerics/(unsigned-byte n)/(signed-byte n)/the array family); an unknown specifier matches nothing. The optional environment is evaluated and ignored: there is one global environment.

The array family is (array ELEMENT-TYPE DIMENSIONS), (simple-array ELEMENT-TYPE DIMENSIONS), (vector ELEMENT-TYPE SIZE) and (simple-vector SIZE) — the specifiers type-of builds. Both halves are checked: the element type against the array's upgraded array-element-type, the dimensions against its own. DIMENSIONS may be a list (* in any position means "any size"), a bare rank, nil for a rank-0 array, or *; both vector spellings pin the rank to 1.

The simple- spellings are strictly SMALLER types, not aliases: simple-array, simple-vector and simple-string additionally require the value to be SIMPLE — no fill pointer, not :adjustable, not displaced — while array, vector and string accept it either way. simple-vector is (simple-array t (*)) exactly, so a string and a packed vector are not one.

A SIZED string specifier — (string n), (simple-string n), and the (vector character n) / (simple-array character (n)) spellings — measures the array DIMENSION, not length. A character vector's length is its fill pointer, so a capacity-4 vector holding one character is a (string 4) and not a (string 1).

A specifier computed at run time is supported too, and takes the same set: an ATOMIC type name (a registered class / struct / condition, a zero-parameter deftype name, or a built-in name), a class metaobject — what find-class and class-of answer designates its own class — or any of the compound specifiers above, whose head and arguments are then read out of the specifier VALUE rather than folded at compile time. So (typep a (type-of a)) answers T for every array shape. class is the class every class metaobject belongs to, so (typep x 'class) is the "is this a class?" test.

A deftype name resolves whether it is spelled at the call site or held in a variable, inside a compound specifier as well. coerce with a computed result type resolves it the same way.