Common Lisp Extensions. Node: Type Predicates

PREV Predicates UP Predicates NEXT Equality Predicates

4.1: Type Predicates

The CL package defines a version of the Common Lisp typep predicate.

Function: typep object type
Check if object is of type type, where type is a (quoted) type name of the sort used by Common Lisp. For example, (typep foo 'integer) is equivalent to (integerp foo).

The type argument to the above function is either a symbol or a list beginning with a symbol.

The following function and macro (not technically predicates) are related to typep.

Function: coerce object type
This function attempts to convert object to the specified type. If object is already of that type as determined by typep, it is simply returned. Otherwise, certain types of conversions will be made: If type is any sequence type (string, list, etc.) then object will be converted to that type if possible. If type is character, then strings of length one and symbols with one-character names can be coerced. If type is float, then integers can be coerced in versions of Emacs that support floats. In all other circumstances, coerce signals an error.
Special Form: deftype name arglist forms...
This macro defines a new type called name. It is similar to defmacro in many ways; when name is encountered as a type name, the body forms are evaluated and should return a type specifier that is equivalent to the type. The arglist is a Common Lisp argument list of the sort accepted by defmacro*. The type specifier `(name args...)' is expanded by calling the expander with those arguments; the type symbol `name' is expanded by calling the expander with no arguments. The arglist is processed the same as for defmacro* except that optional arguments without explicit defaults use * instead of nil as the ``default'' default. Some examples:
(deftype null () '(satisfies null))    ; predefined
(deftype list () '(or null cons))      ; predefined
(deftype unsigned-byte (&optional bits)
  (list 'integer 0 (if (eq bits '*) bits (1- (lsh 1 bits)))))
(unsigned-byte 8)  ==  (integer 0 255)
(unsigned-byte)  ==  (integer 0 *)
unsigned-byte  ==  (integer 0 *)

The last example shows how the Common Lisp unsigned-byte type specifier could be implemented if desired; this package does not implement unsigned-byte by default.

The typecase and check-type macros also use type names. See Conditionals. See Assertions. The map, concatenate, and merge functions take type-name arguments to specify the type of sequence to return. See Sequences.

PREV Predicates UP Predicates NEXT Equality Predicates