# 2.7 类型谓语

当函数被调用时，Emacs Lisp 解释器本身不会对传递给函数的实际参数执行类型检查。它不能这样做，因为 Lisp 中的函数参数没有像其他编程语言中那样的数据类型声明。因此，存在一些函数来测试每个实际参数是否属于该函数可以使用的类型。

所有内置函数都会在适当的时候检查其实际参数的类型，如果参数类型错误，则会发出`wrong-type-argument`错误信号。例如，如果您将`+`无法处理的参数传递给它，则会发生以下情况：

```
(+ 2 'a)
     error→ Wrong type argument: number-or-marker-p, a
```

如果您希望程序以不同方式处理不同类型，则必须进行显式类型检查。检查对象类型的最常见方法是调用*类型谓词*函数。Emacs 对每种类型都有一个类型谓词，还有一些组合类型的谓词。

类型谓词函数接受一个参数；如果参数属于适当的类型，则返回`t`，否则返回`nil`。遵循 Lisp 谓词函数的一般约定，大多数类型谓词的名称以‘p’结尾。

这是一个使用谓词`listp`检查列表和`symbolp`检查符号的示例。

```
(defun add-on (x)
  (cond ((symbolp x)
         ;; 如果 X 是一个符号，将它放入 LIST 。
         (setq list (cons x list)))
        ((listp x)
         ;; 如果 X 是一个列表，则将其元素添加到 LIST。
         (setq list (append x list)))
        (t
         ;; 我们只处理符号和列表。
         (error "Invalid argument %s in add-on" x))))
```

这是一个按字母顺序排列的预定义类型谓词表。

参考 atom. atom

参考 arrayp. arrayp

参考 floatp. bignump

参考 bool-vector-p. bool-vector-p

参考 booleanp. booleanp

参考 bufferp. bufferp

参考 byte-code-function-p. byte-code-function-p

参考 case-table-p. case-table-p

参考 char-or-string-p. char-or-string-p

参考 char-table-p. char-table-p

参考 commandp. commandp

参考 condition-variable-p. condition-variable-p

参考 consp. consp

参考 custom-variable-p. custom-variable-p

参考 floatp. fixnump

参考 floatp. floatp

参考 Low-Level Font. fontp

参考 frame-configuration-p. frame-configuration-p

参考 frame-live-p. frame-live-p

参考 framep. framep

参考 functionp. functionp

参考 hash-table-p. hash-table-p

参考 integer-or-marker-p. integer-or-marker-p

参考 integerp. integerp

参考 keymapp. keymapp

参考 Constant Variables. keywordp

参考 listp. listp

参考 markerp. markerp

参考 mutexp. mutexp

参考 nlistp. nlistp

参考 number-or-marker-p. number-or-marker-p

参考 numberp. numberp

参考 overlayp. overlayp

参考 processp. processp

参考 recordp. recordp

参考 sequencep. sequencep

参考 string-or-null-p. string-or-null-p

参考 stringp. stringp

参考 subrp. subrp

参考 symbolp. symbolp

参考 syntax-table-p. syntax-table-p

参考 threadp. threadp

参考 vectorp. vectorp

参考 wholenump. wholenump

参考 window-configuration-p. window-configuration-p

参考 window-live-p. window-live-p

参考 windowp. windowp

检查对象类型的最通用方法是调用函数`type-of`。回想一下，每个对象都属于一种且只有一种原始类型；`type-of`告诉你是哪一种（参见[Lisp 数据类型](https://www.gnu.org/software/emacs/manual/html_node/elisp/Lisp-Data-Types.html)）。但`type-of`对非原始类型一无所知。在大多数情况下，使用类型谓词比使用`type-of`更方便 。

Function: type-of object

该函数返回一个对象原始类型的符号 。该值是以下符号中的一个`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`，或`window-configuration`。但是，如果object是记录，则返回其第一个槽指定的类型；[记录](https://www.gnu.org/software/emacs/manual/html_node/elisp/Records.html)。

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


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://emacs-lisp.ivory.cafe/lisp-shu-ju-lei-xing/wei-ci-xiang-deng.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
