next up previous
Next: Misellaneous Topics Up: Lisp Objects and Data Previous: Lists and The ``Cons

Run Time Type Identification

The Emacs Lisp interpreter does not do any type checking on the actual arguments passed to functions. It could not do so, as function arguments in Lisp do not have declared data types. This means that a caller can legally call a function with an integer, just as he can with a string literal. Given this scenario, how can the callee ensure or enforce some type semantics for its operation. After all, (factorial ``Sriram Karra'') does not make sense even in a `dynamically typed' with `latent types' language such as Emacs Lisp.

The answer is Type Predicates, or Run Time Type Identification. Lisp provides certain functions (built-in functions in Elisp) that can be used to identify the type of an object at runtime. Examples of such predicates are listp, numberp, stringp, etc.

Type predicates are an extremely handy tool when it comes to customising your Emacs. You will find plenty of Elisp variables that can be set to any number of different types of values, and the responsible package will do interestingly different things in each case. Just to give you a flavour, message-signature is a variable in one of Emacs' mail packages. If this variable is set to t, the contents of file pointed to by message-signature-file will be inserted as your .sig in outgoing mail. If it is assigned a function name, the function is executed and the result is used as the sig. If it is a string literal, the string itself is inserted as the sig. And finally, it can be any Lisp expression, in which case the form is eval'ed and the result is used. How is that for a customisable mail agent :-)


next up previous
Next: Misellaneous Topics Up: Lisp Objects and Data Previous: Lists and The ``Cons
Sriram Karra
2005-01-06