Skip to content

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:

emacs-lisp
(+ 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.

emacs-lisp
(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 bignump.

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.

closurep ​

See closurep.

commandp ​

See commandp.

compiled-function-p ​

See compiled-function-p.

condition-variable-p ​

See condition-variable-p.

consp ​

See consp.

custom-variable-p ​

See custom-variable-p.

fixnump ​

See fixnump.

floatp ​

See floatp.

fontp ​

See Low-Level Font Representation.

frame-configuration-p ​

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

interpreted-function-p ​

See interpreted-function-p.

keymapp ​

See keymapp.

keywordp ​

See Variables that Never Change.

listp ​

See listp.

markerp ​

See markerp.

mutexp ​

See mutexp.

nlistp ​

See nlistp.

number-or-marker-p ​

See number-or-marker-p.

numberp ​

See numberp.

obarrayp ​

See obarrayp.

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 ​

See 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 preferable 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, obarray, 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.

emacs-lisp
(type-of 1)
     ⇒ integer
emacs-lisp
(type-of 'nil)
     ⇒ symbol
(type-of '())    ; () is nil.
     ⇒ symbol
(type-of '(x))
     ⇒ cons
(type-of (record 'foo))
     ⇒ foo

Function: cl-type-of object ​

This function returns a symbol naming the type of object. It usually behaves like type-of, except that it guarantees to return the most precise type possible, which also implies that the specific type it returns may change depending on the Emacs version. For this reason, as a rule you should never compare its return value against some fixed set of types.

emacs-lisp
(cl-type-of 1)
     ⇒ fixnum
emacs-lisp
(cl-type-of 'nil)
     ⇒ null
(cl-type-of (record 'foo))
     ⇒ foo