Special OperatorPackage: exclToCDocOverviewCGDocRelNotesFAQIndexPermutedIndex
Allegro CL version 10.0
Moderate update since the initial 10.0 release.
9.0 version

named-function

Arguments: name lambda-expression

This special operator extension operates approximately as though it were defined by this macro:

 (defmacro excl:named-function (name function) (declare (ignore name)) 
     `(function ,function))

except that when the form is compiled or executed the resultant function is given the specified name. The name has no semantic effect, but may be helpful when encountered in the debugger or in profiler output. Also, if an appropriate function spec handler has been defined, the function name (or its closure template) can be accessed by name for tracing, disassembly, etc. For more info about function specs, see Function specs (fspecs) in implementation.htm.

There is an alternate macro definition for named-function which will be used automatically by macroexpand-1 for the benefit of portable code walkers.

 cl-user(1): (defun make-counter-function (&optional (initial 0)) 
	       (let ((cons (list initial)))
		 (named-function counter-function
		   (lambda () (incf (car cons))))))
 make-counter-function
 cl-user(2): (make-counter-function)
 #<Interpreted Closure counter-function @ #x10022764a2>
 cl-user(3): (funcall *)
 1
 cl-user(4): (funcall **)
 2
 cl-user(5): 

When compiling a named-function form, the compiler treats the form just like a function form, except that the name is used if possible in the resulting function. This is especially useful for closures of a particular kind, as in the following example:

user(1): (defun get-one-arg-frobber (ix) 
            (named-function frobber 
              (lambda (arg) (frob-by-index ix arg)))) 
get-one-arg-frobber 
user(2): (compile *) 
Warning: While compiling these undefined 
functions were referenced: frob-by-index.
get-one-arg-frobber 
nil 
nil 
user(3): (get-one-arg-frobber 10) 
#<Closure frobber @ #x2065dea9> 
user(4): 

Note that the name of the closure is taken from the named-function form. Contrast that with the following definition using the more traditional function form:

user(7): (defun get-one-arg-frobber (ix) 
            (function (lambda (arg)
                         (frob-by-index ix arg)))) 
get-one-arg-frobber 
user(8): (compile *) 
Warning: While compiling these undefined 
functions were referenced: frob-by-index.
get-one-arg-frobber 
nil 
nil 
user(9): (get-one-arg-frobber 12) 
#<Closure (:internal get-one-arg-frobber 0) @ #x20664fa1> 
user(10): 

See also named-annotated-function.


Copyright (c) 1998-2019, Franz Inc. Oakland, CA., USA. All rights reserved.
This page was not revised from the 9.0 page.
Created 2015.5.21.

ToCDocOverviewCGDocRelNotesFAQIndexPermutedIndex
Allegro CL version 10.0
Moderate update since the initial 10.0 release.
9.0 version