elisp: Type Predicates

 
 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’
      Seeatom List-related Predicates.
 
 ‘arrayp’
      Seearrayp Array Functions.
 
 ‘bool-vector-p’
      Seebool-vector-p Bool-Vectors.
 
 ‘bufferp’
      Seebufferp Buffer Basics.
 
 ‘byte-code-function-p’
      Seebyte-code-function-p Byte-Code Type.
 
 ‘case-table-p’
      Seecase-table-p Case Tables.
 
 ‘char-or-string-p’
      Seechar-or-string-p Predicates for Strings.
 
 ‘char-table-p’
      Seechar-table-p Char-Tables.
 
 ‘commandp’
      Seecommandp Interactive Call.
 
 ‘consp’
      Seeconsp List-related Predicates.
 
 ‘custom-variable-p’
      Seecustom-variable-p Variable Definitions.
 
 ‘floatp’
      Seefloatp Predicates on Numbers.
 
 ‘fontp’
      SeeLow-Level Font.
 
 ‘frame-configuration-p’
      Seeframe-configuration-p Frame Configurations.
 
 ‘frame-live-p’
      Seeframe-live-p Deleting Frames.
 
 ‘framep’
      Seeframep Frames.
 
 ‘functionp’
      Seefunctionp Functions.
 
 ‘hash-table-p’
      Seehash-table-p Other Hash.
 
 ‘integer-or-marker-p’
      Seeinteger-or-marker-p Predicates on Markers.
 
 ‘integerp’
      Seeintegerp Predicates on Numbers.
 
 ‘keymapp’
      Seekeymapp Creating Keymaps.
 
 ‘keywordp’
      SeeConstant Variables.
 
 ‘listp’
      Seelistp List-related Predicates.
 
 ‘markerp’
      Seemarkerp Predicates on Markers.
 
 ‘wholenump’
      Seewholenump Predicates on Numbers.
 
 ‘nlistp’
      Seenlistp List-related Predicates.
 
 ‘numberp’
      Seenumberp Predicates on Numbers.
 
 ‘number-or-marker-p’
      Seenumber-or-marker-p Predicates on Markers.
 
 ‘overlayp’
      Seeoverlayp Overlays.
 
 ‘processp’
      Seeprocessp Processes.
 
 ‘sequencep’
      Seesequencep Sequence Functions.
 
 ‘stringp’
      Seestringp Predicates for Strings.
 
 ‘subrp’
      Seesubrp Function Cells.
 
 ‘symbolp’
      Seesymbolp Symbols.
 
 ‘syntax-table-p’
      Seesyntax-table-p Syntax Tables.
 
 ‘vectorp’
      Seevectorp Vectors.
 
 ‘window-configuration-p’
      Seewindow-configuration-p Window Configurations.
 
 ‘window-live-p’
      Seewindow-live-p Deleting Windows.
 
 ‘windowp’
      Seewindowp Basic Windows.
 
 ‘booleanp’
      Seebooleanp nil and t.
 
 ‘string-or-null-p’
      Seestring-or-null-p Predicates for Strings.
 
    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 (SeeLisp 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’, ‘cons’, ‘finalizer’, ‘float’,
      ‘font-entity’, ‘font-object’, ‘font-spec’, ‘frame’, ‘hash-table’,
      ‘integer’, ‘marker’, ‘overlay’, ‘process’, ‘string’, ‘subr’,
      ‘symbol’, ‘vector’, ‘window’, or ‘window-configuration’.
 
           (type-of 1)
                ⇒ integer
           (type-of 'nil)
                ⇒ symbol
           (type-of '())    ; ‘()’ is ‘nil’.
                ⇒ symbol
           (type-of '(x))
                ⇒ cons