unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
From: Jim Porter <jporterbugs@gmail.com>
To: Stefan Monnier <monnier@iro.umontreal.ca>
Cc: 66706@debbugs.gnu.org, mattias.engdegard@gmail.com,
	Po Lu <luangruo@yahoo.com>, Dmitry Gutov <dmitry@gutov.dev>,
	stefankangas@gmail.com, Eli Zaretskii <eliz@gnu.org>
Subject: bug#66706: [PATCH] Automatic elisp dialect insertion
Date: Wed, 25 Oct 2023 20:48:04 -0700	[thread overview]
Message-ID: <ce1da604-8524-7f4c-d017-2a7866c8b729@gmail.com> (raw)
In-Reply-To: <ab030780-8ca6-eb4b-077a-87484db2f0d0@gmail.com>

[-- Attachment #1: Type: text/plain, Size: 515 bytes --]

On 10/25/2023 6:19 PM, Jim Porter wrote:
> I'll start with a patch here then. I think this is also a prime spot to 
> add an example or two that would actually show lexical binding in action 
> (i.e. a sample where the code would do something different under dynamic 
> binding).

Here's a first attempt. I'm not sure I'm entirely happy with it (the 
digression into setting 'lexical-binding' to 't' is a bit disruptive), 
but hopefully it's an improvement. Of course, we can keep adjusting this 
further as needed.

[-- Attachment #2: 0001-Introduce-let-using-lexical-binding-in-the-Lisp-Intr.patch --]
[-- Type: text/plain, Size: 4916 bytes --]

From 6bc9bbbb98105f700bb8d5b04e8de5e261efa777 Mon Sep 17 00:00:00 2001
From: Jim Porter <jporterbugs@gmail.com>
Date: Wed, 25 Oct 2023 20:43:57 -0700
Subject: [PATCH] Introduce 'let' using lexical binding in the Lisp
 Introduction

* doc/lispintro/emacs-lisp-intro.texi (Prevent confusion): Rename to...
(Why Use let?): ... this, and rework the explanation to discuss
lexical binding (including how to enable it).
---
 doc/lispintro/emacs-lisp-intro.texi | 81 +++++++++++++++++++++--------
 1 file changed, 58 insertions(+), 23 deletions(-)

diff --git a/doc/lispintro/emacs-lisp-intro.texi b/doc/lispintro/emacs-lisp-intro.texi
index fce7583fe91..ebbcc08b9ff 100644
--- a/doc/lispintro/emacs-lisp-intro.texi
+++ b/doc/lispintro/emacs-lisp-intro.texi
@@ -3587,39 +3587,74 @@ let
 @code{let} special form prevents this kind of confusion.
 
 @menu
-* Prevent confusion::
+* Why Use let?::
 * Parts of let Expression::
 * Sample let Expression::
 * Uninitialized let Variables::
 @end menu
 
 @ifnottex
-@node Prevent confusion
-@unnumberedsubsec @code{let} Prevents Confusion
+@node Why Use let?
+@unnumberedsubsec Why Use @code{let}?
 @end ifnottex
 
 @cindex @samp{local variable} defined
 @cindex @samp{variable, local}, defined
-The @code{let} special form prevents confusion.  @code{let} creates a
-name for a @dfn{local variable} that overshadows any use of the same
-name outside the @code{let} expression.  This is like understanding
-that whenever your host refers to ``the house'', he means his house, not
-yours.  (Symbols used in argument lists work the same way.
-@xref{defun, , The @code{defun} Macro}.)
-
-Local variables created by a @code{let} expression retain their value
-@emph{only} within the @code{let} expression itself (and within
-expressions called within the @code{let} expression); the local
-variables have no effect outside the @code{let} expression.
-
-Another way to think about @code{let} is that it is like a @code{setq}
-that is temporary and local.  The values set by @code{let} are
-automatically undone when the @code{let} is finished.  The setting
-only affects expressions that are inside the bounds of the @code{let}
-expression.  In computer science jargon, we would say the binding of
-a symbol is visible only in functions called in the @code{let} form;
-in Emacs Lisp, the default scoping is dynamic, not lexical.  (The
-non-default lexical binding is not discussed in this manual.)
+The @code{let} special form provides a way to confine your variables
+to a particular section of your code (in computer science jargon, a
+``scope'').  @code{let} creates a name for a @dfn{local variable} that
+overshadows any use of the same name outside the @code{let} expression
+(we call this ``binding'' the variable).  This prevents any accidental
+usage of these variables outside of the @code{let} expression.  This
+is like understanding that whenever your host refers to ``the house'',
+he means his house, not yours.  (Symbols used in argument lists work
+the same way.  @xref{defun, , The @code{defun} Macro}.)
+
+@cindex lexical binding
+@cindex binding, lexical
+@cindex dynamic binding
+@cindex binding, dynamic
+Before we begin discussing @code{let} in detail, we must first mention
+an important note.  For historical reasons, Emacs Lisp uses a form of
+variable binding called ``dynamic binding''.  However, this manual
+will discuss the preferred form of binding, called ``lexical binding''
+(if you have programmed in other languages before, you're likely
+already familiar with how lexical binding behaves).  In order to use
+lexical binding, you should add something like this to the first line
+of your Emacs Lisp file:
+
+@example
+;;; -*- lexical-binding: t -*-
+@end example
+
+For more information about this, @pxref{Selecting Lisp Dialect, , ,
+elisp, The Emacs Lisp Reference Manual}.
+
+With that out of the way, we can return to discussing @code{let}.
+Local variables created by a @code{let} expression hold their value
+@emph{only} within the body of the @code{let} expression itself; the
+local variables have no effect outside of the @code{let} expression.
+This means that inside the @code{let} body, calling @code{setq}
+for a variable named by the @code{let} expression will set the value
+of the @emph{local} variable of that name.  This also means that
+outside of the @code{let} body, calling @code{setq} for a variable
+named by the @code{let} expression will @emph{not} affect that local
+variable.
+
+For example, if you call a function inside of a @code{let}
+body, that function's body would be unable to ``see'' (or modify) the
+value of a local variable from the @code{let} expression:
+
+@example
+(setq x 1)
+
+(defun getx ()
+  x)
+
+(let ((x 2))
+  (get-x))
+     @result{} 1
+@end example
 
 @code{let} can create more than one variable at once.  Also,
 @code{let} gives each variable it creates an initial value, either a
-- 
2.25.1


  parent reply	other threads:[~2023-10-26  3:48 UTC|newest]

Thread overview: 71+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-10-23 17:46 bug#66706: [PATCH] Automatic elisp dialect insertion Mattias Engdegård
2023-10-23 18:21 ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-10-23 18:44 ` Eli Zaretskii
2023-10-23 19:21   ` Stefan Kangas
2023-10-23 20:20     ` Mattias Engdegård
2023-10-24 17:31     ` Mattias Engdegård
2023-10-24 18:25       ` Eli Zaretskii
2023-10-24 19:19         ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-10-24 20:22           ` Stefan Kangas
2023-10-25  2:31             ` Eli Zaretskii
2023-10-25 11:56               ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-10-25 12:17               ` Stefan Kangas
2023-10-25 12:54                 ` Dmitry Gutov
2023-10-26  0:31                   ` Michael Heerdegen
2023-10-26  6:35                     ` Eli Zaretskii
2023-10-27  3:14                       ` Michael Heerdegen
2023-10-27  6:26                         ` Eli Zaretskii
2023-10-27  7:24                           ` Michael Heerdegen
2023-10-27  7:32                             ` Eli Zaretskii
2023-10-27 14:41                           ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-10-29 12:26                             ` Eli Zaretskii
2023-10-25  0:59           ` Po Lu via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-10-25  1:20             ` Po Lu via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-10-25  2:01             ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-10-25  3:01               ` Po Lu via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-10-25 11:48                 ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-10-25 12:46                   ` Dmitry Gutov
2023-10-25 12:48                   ` Po Lu via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-10-25 14:56                     ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-10-25 16:04                       ` Eli Zaretskii
2023-10-26  0:01                       ` Po Lu via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-10-25 12:03               ` Eli Zaretskii
2023-10-25 13:06                 ` Dmitry Gutov
2023-10-25 13:20                   ` Po Lu via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-10-25 13:40                     ` Dmitry Gutov
2023-10-26  0:07                     ` Jim Porter
2023-10-26  0:40                       ` Po Lu via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-10-26  0:51                       ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-10-26  1:19                         ` Jim Porter
2023-10-26  1:41                           ` Po Lu via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-10-26  3:48                           ` Jim Porter [this message]
2023-10-26  5:56                             ` Jim Porter
2023-10-26  7:09                             ` Eli Zaretskii
2023-10-26  2:37                         ` Drew Adams
2023-10-26  2:28                       ` Drew Adams
2023-10-26  5:21                       ` Eli Zaretskii
2023-10-25 13:57                   ` Eli Zaretskii
2023-10-25 15:11                 ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-10-25 16:08                   ` Eli Zaretskii
2023-10-25 16:10                     ` Dmitry Gutov
2023-10-25 16:20                       ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-10-26  0:02                         ` Po Lu via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-10-25 18:19                     ` Mattias Engdegård
2023-10-25 18:40                       ` Eli Zaretskii
2023-10-25 19:09                         ` Mattias Engdegård
2023-10-25 23:43                           ` Po Lu via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-10-26  0:07                           ` Jim Porter
2023-10-26  2:34                             ` Drew Adams
2023-10-26  3:56                               ` Jim Porter
2023-10-26  5:22                             ` Eli Zaretskii
2023-10-26  6:31                               ` Po Lu via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-10-26 13:54                                 ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-10-26 14:02                                   ` Po Lu via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-10-26 15:35                                     ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-10-26  8:32                             ` Mattias Engdegård
2023-10-26 11:39                               ` Nikolay Kudryavtsev
2023-10-26 15:36                                 ` Drew Adams
2023-10-25 12:36               ` Nikolay Kudryavtsev
2023-10-25 12:48                 ` Dmitry Gutov
2023-10-26 11:06                   ` Nikolay Kudryavtsev
2023-10-25  2:27           ` Eli Zaretskii

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=ce1da604-8524-7f4c-d017-2a7866c8b729@gmail.com \
    --to=jporterbugs@gmail.com \
    --cc=66706@debbugs.gnu.org \
    --cc=dmitry@gutov.dev \
    --cc=eliz@gnu.org \
    --cc=luangruo@yahoo.com \
    --cc=mattias.engdegard@gmail.com \
    --cc=monnier@iro.umontreal.ca \
    --cc=stefankangas@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).