Next: Macro Bindings, Previous: Dynamic Bindings, Up: Variable Bindings
These forms make let-like bindings to functions instead
of variables.
This form establishes
let-style bindings on the function cells of symbols rather than on the value cells. Each binding must be a list of the form ‘(name arglist forms...)’, which defines a function exactly as if it were acl-defunform. The function name is defined accordingly for the duration of the body of thecl-flet; then the old function definition, or lack thereof, is restored.You can use
cl-fletto disable or modify the behavior of functions (including Emacs primitives) in a temporary, localized fashion. (Compare this with the idea of advising functions. See Advising Functions.)The bindings are lexical in scope. This means that all references to the named functions must appear physically within the body of the
cl-fletform.Functions defined by
cl-fletmay use the full Common Lisp argument notation supported bycl-defun; also, the function body is enclosed in an implicit block as if bycl-defun. See Program Structure.Note that the cl.el version of this macro behaves slightly differently. In particular, its binding is dynamic rather than lexical. See Obsolete Macros.
The
cl-labelsform is likecl-flet, except that the function bindings can be recursive. The scoping is lexical, but you can only capture functions in closures iflexical-bindingist. See Closures, and Using Lexical Binding.Lexical scoping means that all references to the named functions must appear physically within the body of the
cl-labelsform. References may appear both in the body forms ofcl-labelsitself, and in the bodies of the functions themselves. Thus,cl-labelscan define local recursive functions, or mutually-recursive sets of functions.A “reference” to a function name is either a call to that function, or a use of its name quoted by
quoteorfunctionto be passed on to, say,mapcar.Note that the cl.el version of this macro behaves slightly differently. See Obsolete Macros.