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: tomas
Subject: Re: Lexical vs. dynamic: small examples?
Date: Sat, 14 Aug 2021 09:35:18 +0200
User-agent: Mutt/1.5.21 (2010-09-15)

On Sat, Aug 14, 2021 at 12:34:31AM -0300, Eduardo Ochs wrote:
> Hi list,
> 
> I am trying to write a section on lexical vs. dynamic binding for a
> tutorial on Emacs Lisp, and I am looking for very short demos that
> show how things work differently in dynamic and in lexical binding...
> 
> Right now what I have is this:
> 
>   http://angg.twu.net/eev-intros/find-lexical-intro.html
>   (find-lexical-intro)

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".

Usually you want both (and civilised languages, like CL, Scheme,
Perl [1] and, of course, Emacs Lisp, the most civilised of all)
do offer facilities for both.

The easiest default, though (for compilers and for users alike)
is lexical binding. Especially if you use other people's code
in yours. Imagine that library `setq'-ing xyzzy "down there",
although xyzzy happens to be an important variable in your
missile-control program (to reuse an already tired analogy ;-)

Of course, civilised library providers wouldn't do that, they
would make sure `xyzzy' is let-bound before `setq'-ing anything.
If the dynamic state doesn't do uncommon things and only walks
the stack "up and down", then it's their variable to `setq',
no matter whether dynamically or lexically.

The corollary is that for most civilised code, there is no
difference whether it is interpreted in a lexical or dynamic
scoping regime.

Stefan, who has taken up the Herculean task of going through
the existing Emacs Lisp code to convert it to lexical sure
has a lot of interesting things to say about that :-)

Cheers

[1] Perl originally had only lexical scope, like any decent
   shell always had. It was with Perl5 (about 1994) that it
   acquired lexical scope (called "my" in Perl-land) as the
   recommended variable localisation strategy.

 - t

Attachment: signature.asc
Description: Digital signature


reply via email to

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