aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGlenn Morris2012-10-27 18:55:40 -0700
committerGlenn Morris2012-10-27 18:55:40 -0700
commit3c0c6155a1ba0179e4099348727ba3243df73be6 (patch)
tree8ff89e89583fafe5357f0fda8ade421db28c6f87
parent82b2101791236f7f0b5ee8a39fb05ab0d5e7834f (diff)
downloademacs-3c0c6155a1ba0179e4099348727ba3243df73be6.tar.gz
emacs-3c0c6155a1ba0179e4099348727ba3243df73be6.zip
Start moving some cl.texi features to an Obsolete appendix
* doc/misc/cl.texi (Lexical Bindings): Move to appendix of obsolete features. (Porting Common Lisp): Emacs Lisp can do true lexical binding now. (Obsolete Features): New appendix. Move Lexical Bindings here.
-rw-r--r--doc/misc/ChangeLog6
-rw-r--r--doc/misc/cl.texi297
2 files changed, 159 insertions, 144 deletions
diff --git a/doc/misc/ChangeLog b/doc/misc/ChangeLog
index 24d8f1cee25..c5d084891f6 100644
--- a/doc/misc/ChangeLog
+++ b/doc/misc/ChangeLog
@@ -1,3 +1,9 @@
12012-10-28 Glenn Morris <rgm@gnu.org>
2
3 * cl.texi (Lexical Bindings): Move to appendix of obsolete features.
4 (Porting Common Lisp): Emacs Lisp can do true lexical binding now.
5 (Obsolete Features): New appendix. Move Lexical Bindings here.
6
12012-10-27 Glenn Morris <rgm@gnu.org> 72012-10-27 Glenn Morris <rgm@gnu.org>
2 8
3 * cl.texi: Use defmac for macros rather than defspec. 9 * cl.texi: Use defmac for macros rather than defspec.
diff --git a/doc/misc/cl.texi b/doc/misc/cl.texi
index 09386e2c0c7..46e9dae5319 100644
--- a/doc/misc/cl.texi
+++ b/doc/misc/cl.texi
@@ -70,6 +70,7 @@ developing GNU and promoting software freedom.''
70* Efficiency Concerns:: Hints and techniques. 70* Efficiency Concerns:: Hints and techniques.
71* Common Lisp Compatibility:: All known differences with Steele. 71* Common Lisp Compatibility:: All known differences with Steele.
72* Porting Common Lisp:: Hints for porting Common Lisp code. 72* Porting Common Lisp:: Hints for porting Common Lisp code.
73* Obsolete Features:: Obsolete features.
73 74
74* GNU Free Documentation License:: The license for this documentation. 75* GNU Free Documentation License:: The license for this documentation.
75* Function Index:: 76* Function Index::
@@ -210,7 +211,6 @@ Internal function and variable names in the package are prefixed
210by @code{cl--}. Here is a complete list of functions prefixed by 211by @code{cl--}. Here is a complete list of functions prefixed by
211@code{cl-} that were not taken from Common Lisp: 212@code{cl-} that were not taken from Common Lisp:
212 213
213@c FIXME lexical-let lexical-let*
214@example 214@example
215cl-callf cl-callf2 cl-defsubst 215cl-callf cl-callf2 cl-defsubst
216cl-floatp-safe cl-letf cl-letf* 216cl-floatp-safe cl-letf cl-letf*
@@ -806,12 +806,11 @@ standard @code{setf} facility, and a number of looping and conditional
806constructs. 806constructs.
807 807
808@c FIXME 808@c FIXME
809@c lexical-let is obsolete; flet is not cl-flet. 809@c flet is not cl-flet, values is not cl-values.
810@c values is not cl-values.
811@menu 810@menu
812* Assignment:: The @code{cl-psetq} form. 811* Assignment:: The @code{cl-psetq} form.
813* Generalized Variables:: Extensions to generalized variables. 812* Generalized Variables:: Extensions to generalized variables.
814* Variable Bindings:: @code{cl-progv}, @code{lexical-let}, @code{flet}, @code{cl-macrolet}. 813* Variable Bindings:: @code{cl-progv}, @code{flet}, @code{cl-macrolet}.
815* Conditionals:: @code{cl-case}, @code{cl-typecase}. 814* Conditionals:: @code{cl-case}, @code{cl-typecase}.
816* Blocks and Exits:: @code{cl-block}, @code{cl-return}, @code{cl-return-from}. 815* Blocks and Exits:: @code{cl-block}, @code{cl-return}, @code{cl-return-from}.
817* Iteration:: @code{cl-do}, @code{cl-dotimes}, @code{cl-dolist}, @code{cl-do-symbols}. 816* Iteration:: @code{cl-do}, @code{cl-dotimes}, @code{cl-dolist}, @code{cl-do-symbols}.
@@ -1422,7 +1421,6 @@ are also related to variable bindings.
1422 1421
1423@menu 1422@menu
1424* Dynamic Bindings:: The @code{cl-progv} form. 1423* Dynamic Bindings:: The @code{cl-progv} form.
1425* Lexical Bindings:: @code{lexical-let} and lexical closures.
1426* Function Bindings:: @code{flet} and @code{labels}. 1424* Function Bindings:: @code{flet} and @code{labels}.
1427* Macro Bindings:: @code{cl-macrolet} and @code{cl-symbol-macrolet}. 1425* Macro Bindings:: @code{cl-macrolet} and @code{cl-symbol-macrolet}.
1428@end menu 1426@end menu
@@ -1447,120 +1445,6 @@ If @var{symbols} is shorter than @var{values}, the excess values
1447are ignored. 1445are ignored.
1448@end defmac 1446@end defmac
1449 1447
1450@node Lexical Bindings
1451@subsection Lexical Bindings
1452
1453@noindent
1454The @code{CL} package defines the following macro which
1455more closely follows the Common Lisp @code{let} form:
1456
1457@defmac lexical-let (bindings@dots{}) forms@dots{}
1458This form is exactly like @code{let} except that the bindings it
1459establishes are purely lexical. Lexical bindings are similar to
1460local variables in a language like C: Only the code physically
1461within the body of the @code{lexical-let} (after macro expansion)
1462may refer to the bound variables.
1463
1464@example
1465(setq a 5)
1466(defun foo (b) (+ a b))
1467(let ((a 2)) (foo a))
1468 @result{} 4
1469(lexical-let ((a 2)) (foo a))
1470 @result{} 7
1471@end example
1472
1473@noindent
1474In this example, a regular @code{let} binding of @code{a} actually
1475makes a temporary change to the global variable @code{a}, so @code{foo}
1476is able to see the binding of @code{a} to 2. But @code{lexical-let}
1477actually creates a distinct local variable @code{a} for use within its
1478body, without any effect on the global variable of the same name.
1479
1480The most important use of lexical bindings is to create @dfn{closures}.
1481A closure is a function object that refers to an outside lexical
1482variable. For example:
1483
1484@example
1485(defun make-adder (n)
1486 (lexical-let ((n n))
1487 (function (lambda (m) (+ n m)))))
1488(setq add17 (make-adder 17))
1489(funcall add17 4)
1490 @result{} 21
1491@end example
1492
1493@noindent
1494The call @code{(make-adder 17)} returns a function object which adds
149517 to its argument. If @code{let} had been used instead of
1496@code{lexical-let}, the function object would have referred to the
1497global @code{n}, which would have been bound to 17 only during the
1498call to @code{make-adder} itself.
1499
1500@example
1501(defun make-counter ()
1502 (lexical-let ((n 0))
1503 (cl-function (lambda (&optional (m 1)) (cl-incf n m)))))
1504(setq count-1 (make-counter))
1505(funcall count-1 3)
1506 @result{} 3
1507(funcall count-1 14)
1508 @result{} 17
1509(setq count-2 (make-counter))
1510(funcall count-2 5)
1511 @result{} 5
1512(funcall count-1 2)
1513 @result{} 19
1514(funcall count-2)
1515 @result{} 6
1516@end example
1517
1518@noindent
1519Here we see that each call to @code{make-counter} creates a distinct
1520local variable @code{n}, which serves as a private counter for the
1521function object that is returned.
1522
1523Closed-over lexical variables persist until the last reference to
1524them goes away, just like all other Lisp objects. For example,
1525@code{count-2} refers to a function object which refers to an
1526instance of the variable @code{n}; this is the only reference
1527to that variable, so after @code{(setq count-2 nil)} the garbage
1528collector would be able to delete this instance of @code{n}.
1529Of course, if a @code{lexical-let} does not actually create any
1530closures, then the lexical variables are free as soon as the
1531@code{lexical-let} returns.
1532
1533Many closures are used only during the extent of the bindings they
1534refer to; these are known as ``downward funargs'' in Lisp parlance.
1535When a closure is used in this way, regular Emacs Lisp dynamic
1536bindings suffice and will be more efficient than @code{lexical-let}
1537closures:
1538
1539@example
1540(defun add-to-list (x list)
1541 (mapcar (lambda (y) (+ x y))) list)
1542(add-to-list 7 '(1 2 5))
1543 @result{} (8 9 12)
1544@end example
1545
1546@noindent
1547Since this lambda is only used while @code{x} is still bound,
1548it is not necessary to make a true closure out of it.
1549
1550You can use @code{defun} or @code{flet} inside a @code{lexical-let}
1551to create a named closure. If several closures are created in the
1552body of a single @code{lexical-let}, they all close over the same
1553instance of the lexical variable.
1554
1555The @code{lexical-let} form is an extension to Common Lisp. In
1556true Common Lisp, all bindings are lexical unless declared otherwise.
1557@end defmac
1558
1559@defmac lexical-let* (bindings@dots{}) forms@dots{}
1560This form is just like @code{lexical-let}, except that the bindings
1561are made sequentially in the manner of @code{let*}.
1562@end defmac
1563
1564@node Function Bindings 1448@node Function Bindings
1565@subsection Function Bindings 1449@subsection Function Bindings
1566 1450
@@ -1650,6 +1534,10 @@ arguments to @code{cl-defmacro} (i.e., a macro name, argument list,
1650and macro-expander forms). The macro is defined accordingly for 1534and macro-expander forms). The macro is defined accordingly for
1651use within the body of the @code{cl-macrolet}. 1535use within the body of the @code{cl-macrolet}.
1652 1536
1537@c FIXME this should be modified to say ``even when lexical-binding
1538@c is code{nil}'', but is that true? The doc of cl-macrolet just
1539@c refers us to cl-flet, which refers to cl-labels, which says that it
1540@c behaves differently according to whether l-b is true or not.
1653Because of the nature of macros, @code{cl-macrolet} is lexically 1541Because of the nature of macros, @code{cl-macrolet} is lexically
1654scoped even in Emacs Lisp: The @code{cl-macrolet} binding will 1542scoped even in Emacs Lisp: The @code{cl-macrolet} binding will
1655affect only calls that appear physically within the body 1543affect only calls that appear physically within the body
@@ -1678,8 +1566,10 @@ I.e., @code{(setq foo 4)} in the above would be equivalent to
1678 1566
1679Likewise, a @code{let} or @code{let*} binding a symbol macro is 1567Likewise, a @code{let} or @code{let*} binding a symbol macro is
1680treated like a @code{letf} or @code{cl-letf*}. This differs from true 1568treated like a @code{letf} or @code{cl-letf*}. This differs from true
1569@c FIXME does it work like this in Emacs with lexical-binding = t?
1681Common Lisp, where the rules of lexical scoping cause a @code{let} 1570Common Lisp, where the rules of lexical scoping cause a @code{let}
1682binding to shadow a @code{cl-symbol-macrolet} binding. In this package, 1571binding to shadow a @code{cl-symbol-macrolet} binding. In this package,
1572@c FIXME obsolete.
1683only @code{lexical-let} and @code{lexical-let*} will shadow a symbol 1573only @code{lexical-let} and @code{lexical-let*} will shadow a symbol
1684macro. 1574macro.
1685 1575
@@ -4861,19 +4751,15 @@ this convention, calls to Lisp builtins like @code{if} and
4861 4751
4862@item 4752@item
4863Lexical scoping. In Common Lisp, function arguments and @code{let} 4753Lexical scoping. In Common Lisp, function arguments and @code{let}
4864bindings apply only to references physically within their bodies 4754bindings apply only to references physically within their bodies (or
4865(or within macro expansions in their bodies). Emacs Lisp, by 4755within macro expansions in their bodies). Traditionally, Emacs Lisp
4866contrast, uses @dfn{dynamic scoping} wherein a binding to a 4756uses @dfn{dynamic scoping} wherein a binding to a variable is visible
4867variable is visible even inside functions called from the body. 4757even inside functions called from the body. Lexical binding is
4868 4758available since Emacs 24.1, so be sure to set @code{lexical-binding}
4869Variables in Common Lisp can be made dynamically scoped by 4759to @code{t} if you need to emulate this aspect of Common Lisp.
4870declaring them @code{special} or using @code{defvar}. In Emacs
4871Lisp it is as if all variables were declared @code{special}.
4872 4760
4873Often you can use code that was written for lexical scoping 4761Here is an example of a Common Lisp code fragment that would fail in
4874even in a dynamically scoped Lisp, but not always. Here is 4762Emacs Lisp if @code{lexical-binding} were set to @code{nil}:
4875an example of a Common Lisp code fragment that would fail in
4876Emacs Lisp:
4877 4763
4878@example 4764@example
4879(defun map-odd-elements (func list) 4765(defun map-odd-elements (func list)
@@ -4886,20 +4772,16 @@ Emacs Lisp:
4886@end example 4772@end example
4887 4773
4888@noindent 4774@noindent
4889In Common Lisp, the two functions' usages of @code{x} are completely 4775With lexical binding, the two functions' usages of @code{x} are
4890independent. In Emacs Lisp, the binding to @code{x} made by 4776completely independent. With dynamic binding, the binding to @code{x}
4891@code{add-odd-elements} will have been hidden by the binding 4777made by @code{add-odd-elements} will have been hidden by the binding
4892in @code{map-odd-elements} by the time the @code{(+ a x)} function 4778in @code{map-odd-elements} by the time the @code{(+ a x)} function is
4893is called. 4779called.
4894 4780
4895(This package avoids such problems in its own mapping functions 4781Internally, this package uses lexical binding so that such problems do
4896by using names like @code{cl--x} instead of @code{x} internally; 4782not occur. @xref{Lexical Bindings}, for a description of the obsolete
4897as long as you don't use this prefix for your own 4783@code{lexical-let} form that emulates a Common Lisp-style lexical
4898variables no collision can occur.) 4784binding when dynamic binding is in use.
4899
4900@xref{Lexical Bindings}, for a description of the @code{lexical-let}
4901form which establishes a Common Lisp-style lexical binding, and some
4902examples of how it differs from Emacs's regular @code{let}.
4903 4785
4904@item 4786@item
4905Reader macros. Common Lisp includes a second type of macro that 4787Reader macros. Common Lisp includes a second type of macro that
@@ -5039,6 +4921,133 @@ note that the current Emacs Lisp compiler does not optimize tail
5039recursion. 4921recursion.
5040@end itemize 4922@end itemize
5041 4923
4924@node Obsolete Features
4925@appendix Obsolete Features
4926
4927This section describes some features of the package that are obsolete
4928and should not be used in new code. They are only provided by the old
4929@file{cl.el} entry point, not by the newer @file{cl-lib.el}.
4930
4931@menu
4932* Lexical Bindings:: An approximation of lexical binding.
4933@end menu
4934
4935@node Lexical Bindings
4936@appendixsec Lexical Bindings
4937
4938The following macros are extensions to Common Lisp, where all bindings
4939are lexical unless declared otherwise. These features are likewise
4940obsolete since the introduction of true lexical binding in Emacs 24.1.
4941
4942@defmac lexical-let (bindings@dots{}) forms@dots{}
4943This form is exactly like @code{let} except that the bindings it
4944establishes are purely lexical.
4945@end defmac
4946
4947@c FIXME remove this and refer to elisp manual.
4948@c Maybe merge some stuff from here to there?
4949@noindent
4950Lexical bindings are similar to local variables in a language like C:
4951Only the code physically within the body of the @code{lexical-let}
4952(after macro expansion) may refer to the bound variables.
4953
4954@example
4955(setq a 5)
4956(defun foo (b) (+ a b))
4957(let ((a 2)) (foo a))
4958 @result{} 4
4959(lexical-let ((a 2)) (foo a))
4960 @result{} 7
4961@end example
4962
4963@noindent
4964In this example, a regular @code{let} binding of @code{a} actually
4965makes a temporary change to the global variable @code{a}, so @code{foo}
4966is able to see the binding of @code{a} to 2. But @code{lexical-let}
4967actually creates a distinct local variable @code{a} for use within its
4968body, without any effect on the global variable of the same name.
4969
4970The most important use of lexical bindings is to create @dfn{closures}.
4971A closure is a function object that refers to an outside lexical
4972variable. For example:
4973
4974@example
4975(defun make-adder (n)
4976 (lexical-let ((n n))
4977 (function (lambda (m) (+ n m)))))
4978(setq add17 (make-adder 17))
4979(funcall add17 4)
4980 @result{} 21
4981@end example
4982
4983@noindent
4984The call @code{(make-adder 17)} returns a function object which adds
498517 to its argument. If @code{let} had been used instead of
4986@code{lexical-let}, the function object would have referred to the
4987global @code{n}, which would have been bound to 17 only during the
4988call to @code{make-adder} itself.
4989
4990@example
4991(defun make-counter ()
4992 (lexical-let ((n 0))
4993 (cl-function (lambda (&optional (m 1)) (cl-incf n m)))))
4994(setq count-1 (make-counter))
4995(funcall count-1 3)
4996 @result{} 3
4997(funcall count-1 14)
4998 @result{} 17
4999(setq count-2 (make-counter))
5000(funcall count-2 5)
5001 @result{} 5
5002(funcall count-1 2)
5003 @result{} 19
5004(funcall count-2)
5005 @result{} 6
5006@end example
5007
5008@noindent
5009Here we see that each call to @code{make-counter} creates a distinct
5010local variable @code{n}, which serves as a private counter for the
5011function object that is returned.
5012
5013Closed-over lexical variables persist until the last reference to
5014them goes away, just like all other Lisp objects. For example,
5015@code{count-2} refers to a function object which refers to an
5016instance of the variable @code{n}; this is the only reference
5017to that variable, so after @code{(setq count-2 nil)} the garbage
5018collector would be able to delete this instance of @code{n}.
5019Of course, if a @code{lexical-let} does not actually create any
5020closures, then the lexical variables are free as soon as the
5021@code{lexical-let} returns.
5022
5023Many closures are used only during the extent of the bindings they
5024refer to; these are known as ``downward funargs'' in Lisp parlance.
5025When a closure is used in this way, regular Emacs Lisp dynamic
5026bindings suffice and will be more efficient than @code{lexical-let}
5027closures:
5028
5029@example
5030(defun add-to-list (x list)
5031 (mapcar (lambda (y) (+ x y))) list)
5032(add-to-list 7 '(1 2 5))
5033 @result{} (8 9 12)
5034@end example
5035
5036@noindent
5037Since this lambda is only used while @code{x} is still bound,
5038it is not necessary to make a true closure out of it.
5039
5040You can use @code{defun} or @code{flet} inside a @code{lexical-let}
5041to create a named closure. If several closures are created in the
5042body of a single @code{lexical-let}, they all close over the same
5043instance of the lexical variable.
5044
5045@defmac lexical-let* (bindings@dots{}) forms@dots{}
5046This form is just like @code{lexical-let}, except that the bindings
5047are made sequentially in the manner of @code{let*}.
5048@end defmac
5049
5050
5042@node GNU Free Documentation License 5051@node GNU Free Documentation License
5043@appendix GNU Free Documentation License 5052@appendix GNU Free Documentation License
5044@include doclicense.texi 5053@include doclicense.texi