Next: Equality Predicates, Previous: Window-System Types, Up: Lisp Data Types [Contents][Index]
The SXEmacs 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: integer-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. SXEmacs 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 only handle 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.
annotationpSee annotationp.
arraypSee arrayp.
atomSee atom.
bit-vector-pSee bit-vector-p.
bitpSee bitp.
boolean-specifier-pSee boolean-specifier-p.
buffer-glyph-pSee buffer-glyph-p.
buffer-live-pSee buffer-live-p.
bufferpSee bufferp.
button-event-pSee button-event-p.
button-press-event-pSee button-press-event-p.
button-release-event-pcase-table-pSee case-table-p.
char-int-pSee char-int-p.
char-or-char-int-pSee char-or-char-int-p.
char-or-string-pSee char-or-string-p.
char-table-pSee char-table-p.
characterpSee characterp.
color-instance-pSee color-instance-p.
color-pixmap-image-instance-pcolor-specifier-pSee color-specifier-p.
commandpSee commandp.
compiled-function-pSee compiled-function-p.
console-live-pSee console-live-p.
consolepSee consolep.
conspSee consp.
database-live-pSee database-live-p.
databasepSee databasep.
device-live-pSee device-live-p.
device-or-frame-pSee device-or-frame-p.
devicepSee devicep.
eval-event-pSee eval-event-p.
event-live-pSee event-live-p.
eventpSee eventp.
extent-live-pSee extent-live-p.
extentpSee extentp.
face-boolean-specifier-pfacepSee facep.
floatpSee floatp.
font-instance-pSee font-instance-p.
font-specifier-pSee font-specifier-p.
frame-live-pSee frame-live-p.
framepSee framep.
functionp(not yet documented)
generic-specifier-pSee generic-specifier-p.
glyphpSee glyphp.
hash-table-pSee hash-table-p.
icon-glyph-pSee icon-glyph-p.
image-instance-pSee image-instance-p.
image-specifier-pSee image-specifier-p.
integer-char-or-marker-pinteger-or-char-pSee integer-or-char-p.
integer-or-marker-pSee integer-or-marker-p.
integer-specifier-pSee integer-specifier-p.
integerpSee integerp.
itimerp(not yet documented)
key-press-event-pSee key-press-event-p.
keymappSee keymapp.
keywordp(not yet documented)
listpSee listp.
markerpSee markerp.
misc-user-event-pSee misc-user-event-p.
mono-pixmap-image-instance-pmotion-event-pSee motion-event-p.
mouse-event-pSee mouse-event-p.
natnum-specifier-pSee natnum-specifier-p.
natnumpSee natnump.
nlistpSee nlistp.
nothing-image-instance-pnumber-char-or-marker-pnumber-or-marker-pSee number-or-marker-p.
numberpSee numberp.
pointer-glyph-pSee pointer-glyph-p.
pointer-image-instance-pprocess-event-pSee process-event-p.
processpSee processp.
range-table-pSee range-table-p.
ringp(not yet documented)
sequencepSee sequencep.
specifierpSee specifierp.
stringpSee stringp.
subrpSee subrp.
subwindow-image-instance-psubwindowpSee subwindowp.
symbolpSee symbolp.
syntax-table-pSee syntax-table-p.
text-image-instance-ptimeout-event-pSee timeout-event-p.
toolbar-button-pSee toolbar-button-p.
toolbar-specifier-pSee toolbar-specifier-p.
user-variable-pSee user-variable-p.
vectorpSee vectorp.
weak-list-pSee weak-list-p.
window-configuration-pwindow-live-pSee window-live-p.
windowpSee 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.
This function returns a symbol naming the primitive type of
object. The value is one of bit-vector, buffer,
char-table, character, charset,
coding-system, cons, color-instance,
compiled-function, console, database,
device, event, extent, face, float,
font-instance, frame, glyph, hash-table,
image-instance, integer, keymap, marker,
process, range-table, specifier, string,
subr, subwindow, symbol, toolbar-button,
vector,
weak-list, window, window-configuration, or
x-resource.
(type-of 1)
⇒ integer
(type-of 'nil)
⇒ symbol
(type-of '()) ; () is nil.
⇒ symbol
(type-of '(x))
⇒ cons
Next: Equality Predicates, Previous: Window-System Types, Up: Lisp Data Types [Contents][Index]