diff options
| author | Jim Porter | 2023-10-25 20:43:57 -0700 |
|---|---|---|
| committer | Jim Porter | 2024-01-09 10:31:06 -0800 |
| commit | d58d0fa52ff22e147b8328759d5f0f762e15bbb5 (patch) | |
| tree | b04b8c222ec615e716bebf5e0321e90e7a5b3cba | |
| parent | 1b123972636d717241a38bcd6daa3e3f424fb8b0 (diff) | |
| download | emacs-d58d0fa52ff22e147b8328759d5f0f762e15bbb5.tar.gz emacs-d58d0fa52ff22e147b8328759d5f0f762e15bbb5.zip | |
Introduce 'let' using lexical binding in the Lisp Introduction
* doc/lispintro/emacs-lisp-intro.texi (Prevent confusion): Rework the
explanation to discuss how things work under lexical binding.
(How let Binds Variables): Describe the differences between lexical
and dynamic binding (including how to configure it).
(defvar): Mention that 'defvar' declares variables as always
dynamically-bound (bug#66756).
| -rw-r--r-- | doc/lispintro/emacs-lisp-intro.texi | 171 |
1 files changed, 149 insertions, 22 deletions
diff --git a/doc/lispintro/emacs-lisp-intro.texi b/doc/lispintro/emacs-lisp-intro.texi index 1e10f62104a..b3fe8ce4589 100644 --- a/doc/lispintro/emacs-lisp-intro.texi +++ b/doc/lispintro/emacs-lisp-intro.texi | |||
| @@ -3556,6 +3556,7 @@ and the two are not intended to refer to the same value. The | |||
| 3556 | * Parts of let Expression:: | 3556 | * Parts of let Expression:: |
| 3557 | * Sample let Expression:: | 3557 | * Sample let Expression:: |
| 3558 | * Uninitialized let Variables:: | 3558 | * Uninitialized let Variables:: |
| 3559 | * How let Binds Variables:: | ||
| 3559 | @end menu | 3560 | @end menu |
| 3560 | 3561 | ||
| 3561 | @ifnottex | 3562 | @ifnottex |
| @@ -3569,24 +3570,26 @@ and the two are not intended to refer to the same value. The | |||
| 3569 | @cindex @samp{variable, local}, defined | 3570 | @cindex @samp{variable, local}, defined |
| 3570 | The @code{let} special form prevents confusion. @code{let} creates a | 3571 | The @code{let} special form prevents confusion. @code{let} creates a |
| 3571 | name for a @dfn{local variable} that overshadows any use of the same | 3572 | name for a @dfn{local variable} that overshadows any use of the same |
| 3572 | name outside the @code{let} expression. This is like understanding | 3573 | name outside the @code{let} expression (in computer science jargon, we |
| 3573 | that whenever your host refers to ``the house'', he means his house, not | 3574 | call this @dfn{binding} the variable). This is like understanding |
| 3574 | yours. (Symbols used in argument lists work the same way. | 3575 | that in your host's home, whenever he refers to ``the house'', he |
| 3576 | means his house, not yours. (The symbols used to name function | ||
| 3577 | arguments are bound as local variables in exactly the same way. | ||
| 3575 | @xref{defun, , The @code{defun} Macro}.) | 3578 | @xref{defun, , The @code{defun} Macro}.) |
| 3576 | 3579 | ||
| 3577 | Local variables created by a @code{let} expression retain their value | 3580 | Another way to think about @code{let} is that it defines a special |
| 3578 | @emph{only} within the @code{let} expression itself (and within | 3581 | region in your code: within the body of the @code{let} expression, the |
| 3579 | expressions called within the @code{let} expression); the local | 3582 | variables you've named have their own local meaning. Outside of the |
| 3580 | variables have no effect outside the @code{let} expression. | 3583 | @code{let} body, they have other meanings (or they may not be defined |
| 3581 | 3584 | at all). This means that inside the @code{let} body, calling | |
| 3582 | Another way to think about @code{let} is that it is like a @code{setq} | 3585 | @code{setq} for a variable named by the @code{let} expression will set |
| 3583 | that is temporary and local. The values set by @code{let} are | 3586 | the value of the @emph{local} variable of that name. However, outside |
| 3584 | automatically undone when the @code{let} is finished. The setting | 3587 | of the @code{let} body (such as when calling a function that was |
| 3585 | only affects expressions that are inside the bounds of the @code{let} | 3588 | defined elsewhere), calling @code{setq} for a variable named by the |
| 3586 | expression. In computer science jargon, we would say the binding of | 3589 | @code{let} expression will @emph{not} affect that local |
| 3587 | a symbol is visible only in functions called in the @code{let} form; | 3590 | variable.@footnote{This describes the behavior of @code{let} when |
| 3588 | in Emacs Lisp, the default scoping is dynamic, not lexical. (The | 3591 | using a style called ``lexical binding'' (@pxref{How let Binds |
| 3589 | non-default lexical binding is not discussed in this manual.) | 3592 | Variables}).} |
| 3590 | 3593 | ||
| 3591 | @code{let} can create more than one variable at once. Also, | 3594 | @code{let} can create more than one variable at once. Also, |
| 3592 | @code{let} gives each variable it creates an initial value, either a | 3595 | @code{let} gives each variable it creates an initial value, either a |
| @@ -3746,6 +3749,128 @@ number is printed in the message using a @samp{%d} rather than a | |||
| 3746 | @samp{%s}.) The four variables as a group are put into a list to | 3749 | @samp{%s}.) The four variables as a group are put into a list to |
| 3747 | delimit them from the body of the @code{let}. | 3750 | delimit them from the body of the @code{let}. |
| 3748 | 3751 | ||
| 3752 | @node How let Binds Variables | ||
| 3753 | @subsection How @code{let} Binds Variables | ||
| 3754 | |||
| 3755 | Emacs Lisp supports two different ways of binding variable names to | ||
| 3756 | their values. These ways affect the parts of your program where a | ||
| 3757 | particular binding is valid. For historical reasons, Emacs Lisp uses | ||
| 3758 | a form of variable binding called @dfn{dynamic binding} by default. | ||
| 3759 | However, in this manual we discuss the preferred form of binding, | ||
| 3760 | called @dfn{lexical binding}, unless otherwise noted (in the future, | ||
| 3761 | the Emacs maintainers plan to change the default to lexical binding). | ||
| 3762 | If you have programmed in other languages before, you're likely | ||
| 3763 | already familiar with how lexical binding behaves. | ||
| 3764 | |||
| 3765 | In order to use lexical binding in a program, you should add this to | ||
| 3766 | the first line of your Emacs Lisp file: | ||
| 3767 | |||
| 3768 | @example | ||
| 3769 | ;;; -*- lexical-binding: t -*- | ||
| 3770 | @end example | ||
| 3771 | |||
| 3772 | For more information about this, @pxref{Selecting Lisp Dialect, , , | ||
| 3773 | elisp, The Emacs Lisp Reference Manual}. | ||
| 3774 | |||
| 3775 | @menu | ||
| 3776 | * Lexical & Dynamic Binding Differences:: | ||
| 3777 | * Lexical vs. Dynamic Binding Example:: | ||
| 3778 | @end menu | ||
| 3779 | |||
| 3780 | @node Lexical & Dynamic Binding Differences | ||
| 3781 | @unnumberedsubsubsec Differences Between Lexical and Dynamic Binding | ||
| 3782 | |||
| 3783 | @cindex Lexical binding | ||
| 3784 | @cindex Binding, lexical | ||
| 3785 | As we discussed before (@pxref{Prevent confusion}), when you create | ||
| 3786 | local variables with @code{let} under lexical binding, those variables | ||
| 3787 | are valid only within the body of the @code{let} expression. In other | ||
| 3788 | parts of your code, they have other meanings, so if you call a | ||
| 3789 | function defined elsewhere within the @code{let} body, that function | ||
| 3790 | would be unable to ``see'' the local variables you've created. (On | ||
| 3791 | the other hand, if you call a function that was defined within a | ||
| 3792 | @code{let} body, that function @emph{would} be able to see---and | ||
| 3793 | modify---the local variables from that @code{let} expression.) | ||
| 3794 | |||
| 3795 | @cindex Dynamic binding | ||
| 3796 | @cindex Binding, dynamic | ||
| 3797 | Under dynamic binding, the rules are different: instead, when you use | ||
| 3798 | @code{let}, the local variables you've created are valid during | ||
| 3799 | execution of the @code{let} expression. This means that, if your | ||
| 3800 | @code{let} expression calls a function, that function can see these | ||
| 3801 | local variables, regardless of where the function is defined | ||
| 3802 | (including in another file entirely). | ||
| 3803 | |||
| 3804 | Another way to think about @code{let} when using dynamic binding is | ||
| 3805 | that every variable name has a global ``stack'' of bindings, and | ||
| 3806 | whenever you use that variable's name, it refers to the binding on the | ||
| 3807 | top of the stack. (You can imagine this like a stack of papers on | ||
| 3808 | your desk with the values written on them.) When you bind a variable | ||
| 3809 | dynamically with @code{let}, it puts the new binding you've specified | ||
| 3810 | on the top of the stack, and then executes the @code{let} body. Once | ||
| 3811 | the @code{let} body finishes, it takes that binding off of the stack, | ||
| 3812 | revealing the one it had (if any) before the @code{let} expression. | ||
| 3813 | |||
| 3814 | @node Lexical vs. Dynamic Binding Example | ||
| 3815 | @unnumberedsubsubsec Example of Lexical vs. Dynamic Binding | ||
| 3816 | In some cases, both lexical and dynamic binding behave identically. | ||
| 3817 | However, in other cases, they can change the meaning of your program. | ||
| 3818 | For example, see what happens in this code under lexical binding: | ||
| 3819 | |||
| 3820 | @example | ||
| 3821 | ;;; -*- lexical-binding: t -*- | ||
| 3822 | |||
| 3823 | (setq x 0) | ||
| 3824 | |||
| 3825 | (defun getx () | ||
| 3826 | x) | ||
| 3827 | |||
| 3828 | (setq x 1) | ||
| 3829 | |||
| 3830 | (let ((x 2)) | ||
| 3831 | (getx)) | ||
| 3832 | @result{} 1 | ||
| 3833 | @end example | ||
| 3834 | |||
| 3835 | @noindent | ||
| 3836 | Here, the result of @code{(getx)} is @code{1}. Under lexical binding, | ||
| 3837 | @code{getx} doesn't see the value from our @code{let} expression. | ||
| 3838 | That's because the body of @code{getx} is outside of the body of our | ||
| 3839 | @code{let} expression. Since @code{getx} is defined at the top, | ||
| 3840 | global level of our code (i.e.@: not inside the body of any @code{let} | ||
| 3841 | expression), it looks for and finds @code{x} at the global level as | ||
| 3842 | well. When executing @code{getx}, the current global value of | ||
| 3843 | @code{x} is @code{1}, so that's what @code{getx} returns. | ||
| 3844 | |||
| 3845 | If we use dynamic binding instead, the behavior is different: | ||
| 3846 | |||
| 3847 | @example | ||
| 3848 | ;;; -*- lexical-binding: nil -*- | ||
| 3849 | |||
| 3850 | (setq x 0) | ||
| 3851 | |||
| 3852 | (defun getx () | ||
| 3853 | x) | ||
| 3854 | |||
| 3855 | (setq x 1) | ||
| 3856 | |||
| 3857 | (let ((x 2)) | ||
| 3858 | (getx)) | ||
| 3859 | @result{} 2 | ||
| 3860 | @end example | ||
| 3861 | |||
| 3862 | @noindent | ||
| 3863 | Now, the result of @code{(getx)} is @code{2}! That's because under | ||
| 3864 | dynamic binding, when executing @code{getx}, the current binding for | ||
| 3865 | @code{x} at the top of our stack is the one from our @code{let} | ||
| 3866 | binding. This time, @code{getx} doesn't see the global value for | ||
| 3867 | @code{x}, since its binding is below the one from our @code{let} | ||
| 3868 | expression in the stack of bindings. | ||
| 3869 | |||
| 3870 | (Some variables are also ``special'', and they are always dynamically | ||
| 3871 | bound even when @code{lexical-binding} is @code{t}. @xref{defvar, , | ||
| 3872 | Initializing a Variable with @code{defvar}}.) | ||
| 3873 | |||
| 3749 | @node if | 3874 | @node if |
| 3750 | @section The @code{if} Special Form | 3875 | @section The @code{if} Special Form |
| 3751 | @findex if | 3876 | @findex if |
| @@ -9101,12 +9226,14 @@ In Emacs Lisp, a variable such as the @code{kill-ring} is created and | |||
| 9101 | given an initial value by using the @code{defvar} special form. The | 9226 | given an initial value by using the @code{defvar} special form. The |
| 9102 | name comes from ``define variable''. | 9227 | name comes from ``define variable''. |
| 9103 | 9228 | ||
| 9104 | The @code{defvar} special form is similar to @code{setq} in that it sets | 9229 | The @code{defvar} special form is similar to @code{setq} in that it |
| 9105 | the value of a variable. It is unlike @code{setq} in two ways: first, | 9230 | sets the value of a variable. It is unlike @code{setq} in three ways: |
| 9106 | it only sets the value of the variable if the variable does not already | 9231 | first, it marks the variable as ``special'' so that it is always |
| 9107 | have a value. If the variable already has a value, @code{defvar} does | 9232 | dynamically bound, even when @code{lexical-binding} is @code{t} |
| 9108 | not override the existing value. Second, @code{defvar} has a | 9233 | (@pxref{How let Binds Variables}). Second, it only sets the value of |
| 9109 | documentation string. | 9234 | the variable if the variable does not already have a value. If the |
| 9235 | variable already has a value, @code{defvar} does not override the | ||
| 9236 | existing value. Third, @code{defvar} has a documentation string. | ||
| 9110 | 9237 | ||
| 9111 | (There is a related macro, @code{defcustom}, designed for variables | 9238 | (There is a related macro, @code{defcustom}, designed for variables |
| 9112 | that people customize. It has more features than @code{defvar}. | 9239 | that people customize. It has more features than @code{defvar}. |