2.7 Type Predicates
The Emacs Lisp interpreter itself does not perform type checking on the actual arguments passed to functions when they are called. It could not do so, since function arguments in Lisp do not have declared data types, as they do in other programming languages. It is therefore up to the individual function to test whether each actual argument belongs to a type that the function can use.
All built-in functions do check the types of their actual arguments when appropriate, and signal a wrong-type-argument error if an argument is of the wrong type. For example, here is what happens if you pass an argument to + that it cannot handle:
(+ 2 'a)
errorβ Wrong type argument: number-or-marker-p, a
If you want your program to handle different types differently, you must do explicit type checking. The most common way to check the type of an object is to call a type predicate function. Emacs has a type predicate for each type, as well as some predicates for combinations of types.
A type predicate function takes one argument; it returns t if the argument belongs to the appropriate type, and nil otherwise. Following a general Lisp convention for predicate functions, most type predicatesβ names end with βpβ.
Here is an example which uses the predicates listp to check for a list and symbolp to check for a symbol.
(defun add-on (x)
(cond ((symbolp x)
;; If X is a symbol, put it on LIST.
(setq list (cons x list)))
((listp x)
;; If X is a list, add its elements to LIST.
(setq list (append x list)))
(t
;; We handle only symbols and lists.
(error "Invalid argument %s in add-on" x))))
Here is a table of predefined type predicates, in alphabetical order, with references to further information.
atomβ
See atom.
arraypβ
See arrayp.
bignumpβ
See floatp.
bool-vector-pβ
See bool-vector-p.
booleanpβ
See booleanp.
bufferpβ
See bufferp.
byte-code-function-pβ
See byte-code-function-p.
case-table-pβ
See case-table-p.
char-or-string-pβ
See char-or-string-p.
char-table-pβ
See char-table-p.
commandpβ
See commandp.
condition-variable-pβ
See condition-variable-p.
conspβ
See consp.
custom-variable-pβ
See custom-variable-p.
fixnumpβ
See floatp.
floatpβ
See floatp.
fontpβ
See Low-Level Font.
frame-configuration-pβ
frame-live-pβ
See frame-live-p.
framepβ
See framep.
functionpβ
See functionp.
hash-table-pβ
See hash-table-p.
integer-or-marker-pβ
See integer-or-marker-p.
integerpβ
See integerp.
keymappβ
See keymapp.
keywordpβ
See Constant Variables.
listpβ
See listp.
markerpβ
See markerp.
mutexpβ
See mutexp.
nlistpβ
See nlistp.
number-or-marker-pβ
See number-or-marker-p.
numberpβ
See numberp.
overlaypβ
See overlayp.
processpβ
See processp.
recordpβ
See recordp.
sequencepβ
See sequencep.
string-or-null-pβ
See string-or-null-p.
stringpβ
See stringp.
subrpβ
See subrp.
symbolpβ
See symbolp.
syntax-table-pβ
See syntax-table-p.
threadpβ
See threadp.
vectorpβ
See vectorp.
wholenumpβ
See wholenump.
window-configuration-pβ
window-live-pβ
See window-live-p.
windowpβ
See windowp.
The most general way to check the type of an object is to call the function type-of. Recall that each object belongs to one and only one primitive type; type-of tells you which one (see Lisp Data Types). But type-of knows nothing about non-primitive types. In most cases, it is more convenient to use type predicates than type-of.
function type-of objectβ
This function returns a symbol naming the primitive type of object. The value is one of the symbols bool-vector, buffer, char-table, compiled-function, condition-variable, cons, finalizer, float, font-entity, font-object, font-spec, frame, hash-table, integer, marker, mutex, overlay, process, string, subr, symbol, thread, vector, window, or window-configuration. However, if object is a record, the type specified by its first slot is returned; Records.
(type-of 1)
β integer
(type-of 'nil)
β symbol
(type-of '()) ; () is nil.
β symbol
(type-of '(x))
β cons
(type-of (record 'foo))
β foo