emacs-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: PATCH: Explicitly show how let works on global-variables


From: Phil Sainty
Subject: Re: PATCH: Explicitly show how let works on global-variables
Date: Wed, 05 Oct 2022 00:36:34 +1300
User-agent: Orcon Webmail

On 2022-10-04 21:09, Pedro Andres Aranda Gutierrez wrote:
I understood as local variable a 'value that was stored in the
function's stack' to be used in the scope of the let. That implied
(once again in my understanding) that the global system-time-locale
would not be affected and hence format-time-string would not see the
change in the value within the let.

Since the addition of lexical binding to Emacs Lisp in Emacs 24.1,
both results are possible depending on whether you are dealing with
a dynamic or a lexical variable.

I.e. given:

 (defun myfunc () foo)
 (let ((foo 'bar)) (myfunc))

If foo is a dynamic variable then the let form will return 'bar.

If foo is a lexical variable, then you'd get this error:
"let: Symbol’s value as variable is void: foo".

Eli quoted the manual:

     Local variables created by a ‘let’ expression retain their value
_only_ within the ‘let’ expression itself (and within expressions called within the ‘let’ expression); the local variables have no effect outside
  the ‘let’ expression.

That "(and within expressions called within the ‘let’ expression)" is
pretty ambiguous wrt dynamic vs lexical binding, and a few lines later
it comments very briefly on this:

  in Emacs Lisp, the default scoping is dynamic, not lexical.
  (The non-default lexical binding is not discussed in this manual.)

Which keeps the rest of the text accurate, yet in an almost-entirely
unexplained manner.

I suggest that at this point it has become pretty necessary for lexical
binding to be discussed in this manual...

* The *scratch* buffer, in which users will perform many if not most of
  their experiments, now uses lexical binding by default.

* If enabled, auto-insert-mode adds lexical-binding: t to new elisp files
  by default.

* IIRC most elisp files in Emacs core are now using lexical binding.

* The emacs-lisp-mode mode-name treats dynamic binding as a warning.

So while it's as true as ever that dynamic binding is the default, the
fact that so many things nowadays default to *enabling* lexical binding
really blurs this line, to the point where I think it's unreasonable to
avoid discussing lexical binding in the introduction to emacs lisp, as
the user will almost unavoidably be exposed to it.

I think examples would be hugely helpful in explaining the difference
between the two types of binding.


-Phil




reply via email to

[Prev in Thread] Current Thread [Next in Thread]