cl: Type Predicates
3.1 Type Predicates
===================
-- Function: cl-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, ‘(cl-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.
• If the type name is a symbol, Emacs appends ‘-p’ to the symbol name
to form the name of a predicate function for testing the type.
(Built-in predicates whose names end in ‘p’ rather than ‘-p’ are
used when appropriate.)
• The type symbol ‘t’ stands for the union of all types. ‘(cl-typep
OBJECT t)’ is always true. Likewise, the type symbol ‘nil’ stands
for nothing at all, and ‘(cl-typep OBJECT nil)’ is always false.
• The type symbol ‘null’ represents the symbol ‘nil’. Thus
‘(cl-typep OBJECT 'null)’ is equivalent to ‘(null OBJECT)’.
• The type symbol ‘atom’ represents all objects that are not cons
cells. Thus ‘(cl-typep OBJECT 'atom)’ is equivalent to ‘(atom
OBJECT)’.
• The type symbol ‘real’ is a synonym for ‘number’, and ‘fixnum’ is a
synonym for ‘integer’.
• The type symbols ‘character’ and ‘string-char’ match integers in
the range from 0 to 255.
• The type list ‘(integer LOW HIGH)’ represents all integers between
LOW and HIGH, inclusive. Either bound may be a list of a single
integer to specify an exclusive limit, or a ‘*’ to specify no
limit. The type ‘(integer * *)’ is thus equivalent to ‘integer’.
• Likewise, lists beginning with ‘float’, ‘real’, or ‘number’
represent numbers of that type falling in a particular range.
• Lists beginning with ‘and’, ‘or’, and ‘not’ form combinations of
types. For example, ‘(or integer (float 0 *))’ represents all
objects that are integers or non-negative floats.
• Lists beginning with ‘member’ or ‘cl-member’ represent objects
‘eql’ to any of the following values. For example, ‘(member 1 2 3
4)’ is equivalent to ‘(integer 1 4)’, and ‘(member nil)’ is
equivalent to ‘null’.
• Lists of the form ‘(satisfies PREDICATE)’ represent all objects for
which PREDICATE returns true when called with that object as an
argument.
The following function and macro (not technically predicates) are
related to ‘cl-typep’.
-- Function: cl-coerce object type
This function attempts to convert OBJECT to the specified TYPE. If
OBJECT is already of that type as determined by ‘cl-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, ‘cl-coerce’ signals an error.
-- Macro: cl-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 ‘cl-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 ‘cl-defmacro’ except that optional arguments without explicit
defaults use ‘*’ instead of ‘nil’ as the “default” default. Some
examples:
(cl-deftype null () '(satisfies null)) ; predefined
(cl-deftype list () '(or null cons)) ; predefined
(cl-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.
DONTPRINTYET The ‘cl-typecase’ (Conditionals) and ‘cl-check-type’ (*noteDONTPRINTYET The ‘cl-typecase’ (Conditionals) and ‘cl-check-type’ (
Assertions) macros also use type names. The ‘cl-map’,
‘cl-concatenate’, and ‘cl-merge’ functions take type-name arguments to
specify the type of sequence to return. Sequences.