[ < ] [ > ]   [ << ] [Plus haut] [ >> ]         [Top] [Table des matières] [Index] [ ? ]

2.6 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

Voir la section atom.

arrayp

Voir la section arrayp.

bool-vector-p

Voir la section bool-vector-p.

bufferp

Voir la section bufferp.

byte-code-function-p

Voir la section byte-code-function-p.

case-table-p

Voir la section case-table-p.

char-or-string-p

Voir la section char-or-string-p.

char-table-p

Voir la section char-table-p.

commandp

Voir la section commandp.

consp

Voir la section consp.

display-table-p

Voir la section display-table-p.

floatp

@xref{Predicates on Numbers, floatp}.

frame-configuration-p

Voir la section frame-configuration-p.

frame-live-p

Voir la section frame-live-p.

framep

Voir la section framep.

functionp

@xref{Functions, functionp}.

hash-table-p

Voir la section hash-table-p.

integer-or-marker-p

Voir la section integer-or-marker-p.

integerp

@xref{Predicates on Numbers, integerp}.

keymapp

Voir la section keymapp.

keywordp

Voir la section Variables that Never Change.

listp

Voir la section listp.

markerp

Voir la section markerp.

wholenump

@xref{Predicates on Numbers, wholenump}.

nlistp

Voir la section nlistp.

numberp

@xref{Predicates on Numbers, numberp}.

number-or-marker-p

Voir la section number-or-marker-p.

overlayp

Voir la section overlayp.

processp

@xref{Processes, processp}.

sequencep

Voir la section sequencep.

stringp

Voir la section stringp.

subrp

Voir la section subrp.

symbolp

@xref{Symbols, symbolp}.

syntax-table-p

@xref{Syntax Tables, syntax-table-p}.

user-variable-p

Voir la section user-variable-p.

vectorp

Voir la section vectorp.

window-configuration-p

Voir la section window-configuration-p.

window-live-p

Voir la section window-live-p.

windowp

Voir la section windowp.

booleanp

@xref{nil and t, booleanp}.

string-or-null-p

Voir la section string-or-null-p.

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 (@pxref{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 symbol, integer, float, string, cons, vector, char-table, bool-vector, hash-table, subr, compiled-function, marker, overlay, window, buffer, frame, process, or window-configuration.

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

[ < ] [ > ]   [ << ] [Plus haut] [ >> ]         [Top] [Table des matières] [Index] [ ? ]

Ce document a été généré par Eric Reinbold le 13 Octobre 2007 en utilisant texi2html 1.78.