Mercurial > hg > xemacs-beta
comparison man/cl.texi @ 5566:4654c01af32b
Improve the implementation, documentation of #'labels, #'flet.
lisp/ChangeLog addition:
2011-09-07 Aidan Kehoe <kehoea@parhasard.net>
* bytecomp.el:
* bytecomp.el (for-effect): Move this earlier in the file, it's
referenced in byte-compile-initial-macro-environment.
* bytecomp.el (byte-compile-initial-macro-environment):
In the byte-compile-macro-environment definition for #'labels, put
off the compiling the lambda bodies until the point where the rest
of the form is being compiled, allowing the lambda bodies to
access appropriate values for byte-compile-bound-variables, and
reducing excessive warning about free variables.
Add a byte-compile-macro-environment definition for #'flet. This
modifies byte-compile-function-environment appropriately, and
warns about bindings of functions that have macro definitions in
the current environment, about functions that have byte codes, and
about functions that have byte-compile methods (which may not do
what the user wants at runtime).
* bytecomp.el (byte-compile-funcall):
If FUNCTION is constant, call #'byte-compile-callargs-warn if
that's appropriate, giving warnings about problems with calling
functions bound with #'labels.
* cl-macs.el:
* cl-macs.el (flet):
Mention the main difference from Common Lisp, that the bindings
are dynamic, not lexical. Counsel the use of #'labels, not #'flet,
for this and other reasons. Explain the limited single use case for
#'flet. Cross-reference to bytecomp.el in a comment.
* cl-macs.el (labels):
Go into detail on which functions may be called from
where. Explain how to access the function definition of a label
within FORM. Add a comment cross-referencing to bytecomp.el.
man/ChangeLog addition:
2011-09-07 Aidan Kehoe <kehoea@parhasard.net>
* cl.texi (Function Bindings):
Move #'labels first, describe it in more detail, explaining that
it is to be preferred over #'flet, and explaining why.
Explain that dynamic bindings with #'flet will also not work when
functions are accessed through their bytecodes.
author | Aidan Kehoe <kehoea@parhasard.net> |
---|---|
date | Wed, 07 Sep 2011 16:26:45 +0100 |
parents | a46c5c8d6564 |
children | bd80d9103fc8 |
comparison
equal
deleted
inserted
replaced
5565:48a3d3281b48 | 5566:4654c01af32b |
---|---|
1797 | 1797 |
1798 @node Function Bindings, Macro Bindings, Lexical Bindings, Variable Bindings | 1798 @node Function Bindings, Macro Bindings, Lexical Bindings, Variable Bindings |
1799 @subsection Function Bindings | 1799 @subsection Function Bindings |
1800 | 1800 |
1801 @noindent | 1801 @noindent |
1802 These forms make @code{let}-like bindings to functions instead | 1802 These forms make @code{let}-like bindings to functions instead of |
1803 of variables. | 1803 variables. Normally you should use @code{labels}, it is less expensive in |
1804 compiled code and avoids the problems of dynamic scope. | |
1805 | |
1806 @defmac labels (bindings@dots{}) forms@dots{} | |
1807 This form establishes @code{lexical-let}-style bindings on the function cells | |
1808 of symbols rather than on their value cells. Each @var{binding} must be a | |
1809 list of the form @samp{(@var{name} @var{arglist} @var{forms}@dots{})}, which | |
1810 defines a function exactly as if it were a @code{defun*} form. The function | |
1811 @var{name} is available within @var{forms}, within the body of @var{name}, and | |
1812 within the body of any other functions in @var{bindings}. This allows the | |
1813 establishment of recursive and mutually-referential functions. | |
1814 | |
1815 These functions are not available by name at run-time to code textually | |
1816 outside of the @code{labels} form, though they may be passed to other code by | |
1817 value. Since @code{labels} makes lexical rather than dynamic bindings, | |
1818 bindings of functions like @code{+} and @code{list} that have byte codes will | |
1819 succeed---that is, calls to such functions within @var{form} will reflect the | |
1820 bindings within the @code{labels} form, something not true of @code{flet}, | |
1821 which see. | |
1822 | |
1823 Within @var{forms}, to access a bound function as a callable object, quote its | |
1824 name using @var{#'name}, as in the following example. | |
1825 | |
1826 @example | |
1827 (labels ((1+ (number) | |
1828 "Return 1.0 added to NUMBER" | |
1829 (+ number 1.0))) | |
1830 (map 'vector #'1+ '(10 9 8 7 6 5 4 3 2 1))) | |
1831 @result{} [11.0 10.0 9.0 8.0 7.0 6.0 5.0 4.0 3.0 2.0] | |
1832 @end example | |
1833 | |
1834 Functions defined by @code{labels} may use the full Common Lisp argument | |
1835 notation supported by @code{defun*}; also, the function body is enclosed in an | |
1836 implicit block as if by @code{defun*}. @xref{Program Structure}. | |
1837 @end defmac | |
1804 | 1838 |
1805 @defmac flet (bindings@dots{}) forms@dots{} | 1839 @defmac flet (bindings@dots{}) forms@dots{} |
1806 This form establishes @code{let}-style bindings on the function | 1840 This form establishes @code{let}-style bindings on the function cells of |
1807 cells of symbols rather than on the value cells. Each @var{binding} | 1841 symbols rather than on the value cells. In contravention of Common Lisp, |
1808 must be a list of the form @samp{(@var{name} @var{arglist} | 1842 Emacs Lisp @code{flet} establishes dynamic bindings (available at runtime) |
1809 @var{forms}@dots{})}, which defines a function exactly as if | 1843 rather than lexical (available at compile time, but outside of @var{forms}, |
1810 it were a @code{defun*} form. The function @var{name} is defined | 1844 not at runtime). The result is that @code{flet} affects indirect calls to a |
1811 accordingly for the duration of the body of the @code{flet}; then | 1845 function as well as calls directly inside the @code{flet} form itself. |
1812 the old function definition, or lack thereof, is restored. | 1846 |
1813 | 1847 You can use @code{flet} to disable or modify the behavior of a function in a |
1814 While @code{flet} in Common Lisp establishes a lexical binding of | 1848 temporary fashion. This will even work on XEmacs primitives, although note |
1815 @var{name}, Emacs Lisp @code{flet} makes a dynamic binding. The | 1849 that some calls to primitive functions internal to XEmacs are made without |
1816 result is that @code{flet} affects indirect calls to a function as | 1850 going through the symbol's function cell, and so will not be affected by |
1817 well as calls directly inside the @code{flet} form itself. | 1851 @code{flet}. For example, |
1818 | |
1819 You can use @code{flet} to disable or modify the behavior of a | |
1820 function in a temporary fashion. This will even work on Emacs | |
1821 primitives, although note that some calls to primitive functions | |
1822 internal to Emacs are made without going through the symbol's | |
1823 function cell, and so will not be affected by @code{flet}. For | |
1824 example, | |
1825 | 1852 |
1826 @example | 1853 @example |
1827 (flet ((message (&rest args) (push args saved-msgs))) | 1854 (flet ((message (&rest args) (push args saved-msgs))) |
1828 (do-something)) | 1855 (do-something)) |
1829 @end example | 1856 @end example |
1830 | 1857 |
1831 This code attempts to replace the built-in function @code{message} | 1858 This code attempts to replace the built-in function @code{message} with a |
1832 with a function that simply saves the messages in a list rather | 1859 function that simply saves the messages in a list rather than displaying them. |
1833 than displaying them. The original definition of @code{message} | 1860 The original definition of @code{message} will be restored after |
1834 will be restored after @code{do-something} exits. This code will | 1861 @code{do-something} exits. This code will work fine on messages generated by |
1835 work fine on messages generated by other Lisp code, but messages | 1862 other Lisp code, but messages generated directly inside XEmacs will not be |
1836 generated directly inside Emacs will not be caught since they make | 1863 caught since they make direct C-language calls to the message routines rather |
1837 direct C-language calls to the message routines rather than going | 1864 than going through the Lisp @code{message} function. |
1838 through the Lisp @code{message} function. | 1865 |
1839 | 1866 This is equally true for functions with associated byte codes, since they are |
1840 Functions defined by @code{flet} may use the full Common Lisp | 1867 also not accessed through the Lisp function slot. The byte compiler will |
1841 argument notation supported by @code{defun*}; also, the function | 1868 warn in both these cases. |
1842 body is enclosed in an implicit block as if by @code{defun*}. | |
1843 @xref{Program Structure}. | |
1844 @end defmac | |
1845 | |
1846 @defmac labels (bindings@dots{}) forms@dots{} | |
1847 The @code{labels} form is a synonym for @code{flet}. (In Common | |
1848 Lisp, @code{labels} and @code{flet} differ in ways that depend on | |
1849 their lexical scoping; these distinctions vanish in dynamically | |
1850 scoped Emacs Lisp.) | |
1851 @end defmac | 1869 @end defmac |
1852 | 1870 |
1853 @node Macro Bindings, , Function Bindings, Variable Bindings | 1871 @node Macro Bindings, , Function Bindings, Variable Bindings |
1854 @subsection Macro Bindings | 1872 @subsection Macro Bindings |
1855 | 1873 |