The function definition of a symbol is the object stored in the function cell of the symbol. The functions described here access, test, and set the function cell of symbols.
See also the function indirect-function
in section Symbol Function Indirection.
void-function
error is
signaled.
This function does not check that the returned object is a legitimate function.
(defun bar (n) (+ n 2)) => bar (symbol-function 'bar) => (lambda (n) (+ n 2)) (fset 'baz 'bar) => bar (symbol-function 'baz) => bar
If you have never given a symbol any function definition, we say that
that symbol's function cell is void. In other words, the function
cell does not have any Lisp object in it. If you try to call such a symbol
as a function, it signals a void-function
error.
Note that void is not the same as nil
or the symbol
void
. The symbols nil
and void
are Lisp objects,
and can be stored into a function cell just as any other object can be
(and they can be valid functions if you define them in turn with
defun
). A void function cell contains no object whatsoever.
You can test the voidness of a symbol's function definition with
fboundp
. After you have given a symbol a function definition, you
can make it void once more using fmakunbound
.
t
if the symbol has an object in its
function cell, nil
otherwise. It does not check that the object
is a legitimate function.
void-function
error. (See also makunbound
, in section When a Variable is "Void".)
(defun foo (x) x) => foo (foo 1) =>1 (fmakunbound 'foo) => foo (foo 1) error--> Symbol's function definition is void: foo
There are three normal uses of this function:
defalias
instead of
fset
; see section Defining Functions.)
defun
. For example, you can use fset
to give a symbol s1
a function definition which is another symbol
s2
; then s1
serves as an alias for whatever definition
s2
presently has. (Once again use defalias
instead of
fset
if you think of this as the definition of s1
.)
defun
were not a primitive, it could be written in Lisp (as a macro) using
fset
.
Here are examples of these uses:
;; Savefoo
's definition inold-foo
. (fset 'old-foo (symbol-function 'foo)) ;; Make the symbolcar
the function definition ofxfirst
. ;; (Most likely,defalias
would be better thanfset
here.) (fset 'xfirst 'car) => car (xfirst '(1 2 3)) => 1 (symbol-function 'xfirst) => car (symbol-function (symbol-function 'xfirst)) => #<subr car> ;; Define a named keyboard macro. (fset 'kill-two-lines "\^u2\^k") => "\^u2\^k" ;; Here is a function that alters other functions. (defun copy-function-definition (new old) "Define NEW with the same function definition as OLD." (fset new (symbol-function old)))
When writing a function that extends a previously defined function, the following idiom is sometimes used:
(fset 'old-foo (symbol-function 'foo)) (defun foo () "Just like old-foo, except more so." (old-foo) (more-so))
This does not work properly if foo
has been defined to autoload.
In such a case, when foo
calls old-foo
, Lisp attempts
to define old-foo
by loading a file. Since this presumably
defines foo
rather than old-foo
, it does not produce the
proper results. The only way to avoid this problem is to make sure the
file is loaded before moving aside the old definition of foo
.
But it is unmodular and unclean, in any case, for a Lisp file to redefine a function defined elsewhere. It is cleaner to use the advice facility (see section Advising Emacs Lisp Functions).
Go to the first, previous, next, last section, table of contents.