help-gnu-emacs
[Top][All Lists]
Advanced

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

Re: Lexical vs. dynamic: small examples?


From: Eduardo Ochs
Subject: Re: Lexical vs. dynamic: small examples?
Date: Sat, 14 Aug 2021 13:00:34 -0300

On Sat, 14 Aug 2021 at 04:35, <tomas@tuxteam.de> wrote:
>
> Hm. I have the feeling that it'll difficult to appreciate the
> differences between lexical and dynamic bindings whithin such
> short snippets. You don't have much room to build up a lexical
> environment worth its salt :-)
>
> The metaphor which, for me, did "click" was: lexical binding is
> a binding along "space", dynamic binding along "time".


Hi Tomas,

I forgot to explain some things about the style of that tutorial... I
thought that they would be obvious, but they're not.

The two programming languages that I use more are Elisp and Lua, and
Lua only has lexical binding... so I know how lexical binding works,
how to use it, and what are its advantages, but its exact semantics in
Lua only became clear to me when I learned 1) how to inspect all the
"local variables" of a running Lua function - in Lua this includes the
arguments that the function received, and 2) how some local variables
are "captured" to become upvales. This is explained in pages 8 and 9
here:

  https://web.tecgraf.puc-rio.br/~lhf/ftp/doc/jucs05.pdf#page=9

My main intent with eev since the beginning (in the mid-90s) has
always been to create my own "executable notes" and share them, and to
make other people create and share their executable notes too... there
seemed to be some kind of taboo against that at that time: people
shared textual explanations happily but hesitated an almost infinite
number of times before sharing runnable snippets, and I was tired of
spending hours every time undoing their translations from snippets to
textual explanations to reconstruct the runnable snippets that they
didn't want to share...

Two days ago someone on IRC told me that the documentation of Common
Lisp has great explanations of how lexical bindings works on CL, and
that lots of it applied to lexical binding in Emacs too. I forgot to
ask for links - when I get them I will add them to my tutorial,
probably just preceding them by a "See:", because I am trying to keep
my own textual explanations very short.

This page

  http://www.gnu.org/software/emacs/manual/html_node/elisp/Closures.html
  (find-elnode "Closures")

says:

  However, the fact that the internal structure of a closure is
  exposed to the rest of the Lisp world is considered an internal
  implementation detail. For this reason, we recommend against
  directly examining or altering the structure of closure objects.

but for me, and for many people that I know, the easiest way to
understand a specification that can be implemented in many ways is to
study the specification AND one implementation of it. Snippets that
show the "internal implementation details" of the current
implementation of lexical binding in Emacs are hard to find, so I'm
writing them myself - and asking for help. Eev has several kinds of
hyperlinks to source code, like this one,

  ;; (find-efunction 'defvar "declared_special")

that finds the definition of defvar - a DEFUN ("defvar", ...) - in the
C source of Emacs and searches for string "declared_special" in it,
and I am using them in my notes and tutorials to point the primary
source of information about the implementation details - the source
code! - instead of writing my own textual explanations.

So, that's why I was so happy when I found the getter/setter example
that is here:

  http://angg.twu.net/eev-intros/find-lexical-intro.html#3
  (find-lexical-intro "3. `get/set'")

                    https://0x0.st/-JRW.bin
  (find-wget-elisp "https://0x0.st/-JRW.bin";)

It has some code that was quite mysterious to me until a few days ago
- the (defun get/set0 ...), that buils two closures that share the
same lexical environment - and it shows how that code gets translated
to lower-level elisp code that I could understand.

  Cheers,
    Eduardo Ochs
    http://angg.twu.net/#eev
    http://angg.twu.net/eev-intros/find-elisp-intro.html
    http://angg.twu.net/eev-intros/find-lexical-intro.html



reply via email to

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