unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
From: Michael Albinus <michael.albinus@gmx.de>
To: Jim Porter <jporterbugs@gmail.com>
Cc: eliz@gnu.org, rms@gnu.org, 66756@debbugs.gnu.org
Subject: bug#66756: 30.0.50; [PATCH] Improve discussion of 'let' in Elisp Introduction manual
Date: Sun, 19 Nov 2023 09:38:54 +0100	[thread overview]
Message-ID: <878r6ut9wh.fsf@gmx.de> (raw)
In-Reply-To: <f44cca7f-13bb-a41a-c9ce-55f1b736c52b@gmail.com> (Jim Porter's message of "Sat, 18 Nov 2023 21:30:11 -0800")

Jim Porter <jporterbugs@gmail.com> writes:

Hi Jim,

> +In some cases, both lexical and dynamic binding behave identically.
> +However, in other cases, they can change the meaning of your program.
> +For example, under lexical binding, 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
> +;;; -*- lexical-binding: t -*-
> +
> +(setq x 1)
> +
> +(defun getx ()
> +  x)
> +
> +(let ((x 2))
> +  (getx))
> +     @result{} 1
> +@end example
> +
> +@noindent
> +If we use dynamic binding instead, the behavior is different:
> +
> +@example
> +;;; -*- lexical-binding: nil -*-
> +
> +(setq x 1)
> +
> +(defun getx ()
> +  x)
> +
> +(let ((x 2))
> +  (getx))
> +     @result{} 2
> +@end example
> +
> +Now, the result of @samp{(getx)} is @samp{2}!  That's because under
> +dynamic binding, when @code{getx} looks for the value of @code{x}, it
> +sees the value we set in our @code{let} expression.  In other words,
> +the call to @code{getx} happens during the @emph{time} when our
> +@code{let} expression is active.  Under lexical binding, @code{getx}
> +doesn't see the value from our @code{let} expression.  That's because
> +it happens in a different @emph{place} than the @code{let} body.

Would it be worth to emphasize, that a declaration of x changes this?
That is, when a variable is declared, both lexical and dynamic binding
behave identically.

@example
;;; -*- lexical-binding: t -*-

(devfar x 1)

(defun getx ()
  x)

(let ((x 2))
  (getx))
     @result{} 2
@end example

Best regards, Michael.





  reply	other threads:[~2023-11-19  8:38 UTC|newest]

Thread overview: 33+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-10-26  5:54 bug#66756: 30.0.50; [PATCH] Improve discussion of 'let' in Elisp Introduction manual Jim Porter
2023-10-26 18:30 ` Jim Porter
2023-10-29 16:38   ` Richard Stallman
2023-10-29 17:18     ` Drew Adams
2023-11-18  2:09     ` Jim Porter
2023-11-19  3:39       ` Richard Stallman
2023-11-19  5:25         ` Jim Porter
2023-11-19  5:30           ` Jim Porter
2023-11-19  8:38             ` Michael Albinus [this message]
2023-11-19 20:17               ` Jim Porter
2023-11-19 23:05                 ` Jim Porter
2023-11-20 13:28                   ` Michael Albinus
2023-11-23  2:57             ` Richard Stallman
2023-11-23 21:04               ` Jim Porter
2023-11-24  7:06                 ` Eli Zaretskii
2023-11-24  9:01                   ` Jim Porter
2023-11-24 11:41                     ` Eli Zaretskii
2023-11-24 21:46                       ` Jim Porter
2023-11-25  7:51                         ` Eli Zaretskii
2023-11-30 21:03                           ` Jim Porter
2023-12-01  8:29                             ` Eli Zaretskii
2023-12-04  3:08                               ` Richard Stallman
2023-12-04  3:08                             ` Richard Stallman
2023-12-04  4:34                               ` Jim Porter
2023-12-10 19:36                                 ` Jim Porter
2023-12-16 23:10                                   ` Stefan Kangas
2023-12-17 20:47                                     ` Jim Porter
2024-01-09 18:40                                       ` Jim Porter
2023-12-04  3:08                             ` Richard Stallman
2023-11-04  8:27   ` Eli Zaretskii
2023-11-04 16:44     ` Jim Porter
2023-11-06  2:29 ` Richard Stallman
2023-11-06  2:29 ` Richard Stallman

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=878r6ut9wh.fsf@gmx.de \
    --to=michael.albinus@gmx.de \
    --cc=66756@debbugs.gnu.org \
    --cc=eliz@gnu.org \
    --cc=jporterbugs@gmail.com \
    --cc=rms@gnu.org \
    /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).