* Lexical vs. dynamic: small examples?
@ 2021-08-14 3:34 Eduardo Ochs
2021-08-14 3:56 ` Emanuel Berg via Users list for the GNU Emacs text editor
` (2 more replies)
0 siblings, 3 replies; 20+ messages in thread
From: Eduardo Ochs @ 2021-08-14 3:34 UTC (permalink / raw)
To: help-gnu-emacs
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)
I have tried really hard to the make its section "0. How to use this",
that is at:
http://angg.twu.net/eev-intros/find-lexical-intro.html#0
(find-lexical-intro "0. How to use this")
both clear AND useful to people who do not use eev, and I think that
the big example in section 3,
http://angg.twu.net/eev-intros/find-lexical-intro.html#3
(find-lexical-intro "3. `get/set'")
is quite nice - especially because of its last part, that inspects two
different getter-setter pairs, shows that their lexical environments
are the cadrs of their closures, and shows that `geta' and `seta'
share the same lexical environment and that `getb' and `setb' share
another lexical environment...
My current knowledge of lexical binding stops there, though. Any
comments (or help) would be very welcome...
Cheers,
Eduardo Ochs
http://angg.twu.net/#eev
^ permalink raw reply [flat|nested] 20+ messages in thread
* Re: Lexical vs. dynamic: small examples?
2021-08-14 3:34 Eduardo Ochs
@ 2021-08-14 3:56 ` Emanuel Berg via Users list for the GNU Emacs text editor
2021-08-14 4:12 ` Eduardo Ochs
2021-08-14 7:35 ` tomas
2021-08-14 19:00 ` Gregory Heytings
2 siblings, 1 reply; 20+ messages in thread
From: Emanuel Berg via Users list for the GNU Emacs text editor @ 2021-08-14 3:56 UTC (permalink / raw)
To: help-gnu-emacs
Eduardo Ochs wrote:
> is quite nice - especially because of its last part, that
> inspects two different getter-setter pairs, shows that their
> lexical environments are the cadrs of their closures, and
> shows that `geta' and `seta' share the same lexical
> environment and that `getb' and `setb' share another lexical
> environment...
>
> My current knowledge of lexical binding stops there, though.
> Any comments (or help) would be very welcome...
OK, IIUC with lexical (AKA static) binding the variable's
value is determined by the code and the scope, so you can
always find out what value it is by looking at that and move
within that and only that delimited area.
Move outside of that the variable bindings don't mean
anything. Move from A to B, and you wanna know what goes on in
B, it doesn't matter what happened in A and you look for the
answer in BB (B and only B).
Have a look:
(let ((a 1))
(do-something a) ; 1 is here
(do-something-else) ) ; but if a is referenced here, it isn't 1
; not the same a!
This is more useful and clear to the regular user and perhaps
the advanced one as well and most people should think this
natural from experience but also from what makes sense looking
at that code :) This style seems to make for more independent,
well-defined, more reusable and less vulnerable units of
computation...
With dynamic binding however the variable's value is rather
read from the top of a stack data structure, everyone who sets
the value then pushes it onto the top of the stack - and when
it is unset that value is popped - but even so if it is
thereafter referenced, it is still defined, one just looks for
the plate below.
This style is more like one mastermind keeping track of
everything thru telepathy...
--
underground experts united
https://dataswamp.org/~incal
^ permalink raw reply [flat|nested] 20+ messages in thread
* Re: Lexical vs. dynamic: small examples?
2021-08-14 3:56 ` Emanuel Berg via Users list for the GNU Emacs text editor
@ 2021-08-14 4:12 ` Eduardo Ochs
0 siblings, 0 replies; 20+ messages in thread
From: Eduardo Ochs @ 2021-08-14 4:12 UTC (permalink / raw)
To: Emanuel Berg, help-gnu-emacs
On Sat, 14 Aug 2021 at 00:56, Emanuel Berg via Users list for the GNU
Emacs text editor <help-gnu-emacs@gnu.org> wrote:
>
> Have a look:
>
> (let ((a 1))
> (do-something a) ; 1 is here
> (do-something-else) ) ; but if a is referenced here, it isn't 1
> ; not the same a!
Hi Emanuel,
Thanks! I converted your idea to this...
(setq a 42)
(defun *a10 () (* a 10))
(let ((a 99))
(list (* a 10) (*a10))
)
(let ((b 99))
(list (* b 10) (*a10))
)
Cheers =),
Eduardo Ochs
^ permalink raw reply [flat|nested] 20+ messages in thread
* Re: Lexical vs. dynamic: small examples?
2021-08-14 3:34 Eduardo Ochs
2021-08-14 3:56 ` Emanuel Berg via Users list for the GNU Emacs text editor
@ 2021-08-14 7:35 ` tomas
2021-08-14 16:00 ` Eduardo Ochs
2021-08-14 19:31 ` Emanuel Berg via Users list for the GNU Emacs text editor
2021-08-14 19:00 ` Gregory Heytings
2 siblings, 2 replies; 20+ messages in thread
From: tomas @ 2021-08-14 7:35 UTC (permalink / raw)
To: help-gnu-emacs
[-- Attachment #1: Type: text/plain, Size: 2122 bytes --]
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 #2: Digital signature --]
[-- Type: application/pgp-signature, Size: 198 bytes --]
^ permalink raw reply [flat|nested] 20+ messages in thread
* Re: Lexical vs. dynamic: small examples?
2021-08-14 7:35 ` tomas
@ 2021-08-14 16:00 ` Eduardo Ochs
2021-08-14 19:41 ` Emanuel Berg via Users list for the GNU Emacs text editor
2021-08-14 20:42 ` tomas
2021-08-14 19:31 ` Emanuel Berg via Users list for the GNU Emacs text editor
1 sibling, 2 replies; 20+ messages in thread
From: Eduardo Ochs @ 2021-08-14 16:00 UTC (permalink / raw)
To: tomas; +Cc: help-gnu-emacs
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
^ permalink raw reply [flat|nested] 20+ messages in thread
* Re: Lexical vs. dynamic: small examples?
2021-08-14 3:34 Eduardo Ochs
2021-08-14 3:56 ` Emanuel Berg via Users list for the GNU Emacs text editor
2021-08-14 7:35 ` tomas
@ 2021-08-14 19:00 ` Gregory Heytings
2021-08-14 20:16 ` Emanuel Berg via Users list for the GNU Emacs text editor
2021-08-14 20:41 ` tomas
2 siblings, 2 replies; 20+ messages in thread
From: Gregory Heytings @ 2021-08-14 19:00 UTC (permalink / raw)
To: Eduardo Ochs; +Cc: help-gnu-emacs
>
> 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...
>
Suppose you write a function to remove unnecessary whitespaces at the end
of lines:
(defun delete-whitespace-at-eol ()
(interactive)
(save-excursion (replace-regexp " *$" "" nil (point-min) (point-max))))
Now if you M-x trim-whitespaces-at-eol in a read-only buffer, you'll get a
"Buffer is read-only" error. Suppose you want to make that function work
for read-only buffers. If Emacs Lisp only had lexical binding, you would
have to alter the global "buffer-read-only" variable, that is, to do
something like:
(defvar saved-buffer-read-only)
(defun delete-whitespace-at-eol ()
(interactive)
(setq saved-buffer-read-only buffer-read-only)
(setq buffer-read-only nil)
(save-excursion (replace-regexp " *$" "" nil (point-min) (point-max)))
(setq buffer-read-only saved-buffer-read-only))
This becomes much simpler with dynamic binding:
(defun delete-whitespace-at-eol ()
(interactive)
(let ((buffer-read-only nil))
(save-excursion (replace-regexp " *$" "" nil (point-min) (point-max)))))
The "let ((buffer-read-only nil))" creates a new "buffer-read-only"
variable and sets it to nil. When "replace-regexp", and the functions
called by "replace-regexp", consult the value of the "buffer-read-only"
variable, they will see the "buffer-read-only" variable created in
"delete-whitespace-at-eol", and its value "nil", instead of the global
"buffer-read-only" variable.
It is easier to think that "let ((buffer-read-only nil))" creates a new
"buffer-read-only" variable, but technically this is not correct. What
happens instead is that the old value of "buffer-read-only" is saved, the
value of "buffer-read-only" is updated, and upon returning from the "let",
the saved value is restored.
^ permalink raw reply [flat|nested] 20+ messages in thread
* Re: Lexical vs. dynamic: small examples?
2021-08-14 7:35 ` tomas
2021-08-14 16:00 ` Eduardo Ochs
@ 2021-08-14 19:31 ` Emanuel Berg via Users list for the GNU Emacs text editor
2021-08-14 20:31 ` tomas
1 sibling, 1 reply; 20+ messages in thread
From: Emanuel Berg via Users list for the GNU Emacs text editor @ 2021-08-14 19:31 UTC (permalink / raw)
To: help-gnu-emacs
tomas wrote:
> The metaphor which, for me, did "click" was: lexical binding
> is a binding along "space", dynamic binding along "time".
Okay, but as for dynamic, isn't that the definition rather
than a metaphor?
"Lexical" refers to how the code is written - in particular
where a variable is defined and how it is scoped in the
source, when the source is compiled.
Lexical binding (or scope) could just as well be called
static scoping.
Dynamic or lexical binding answers the question how a variable
is looked up when it is referenced, i.e. called by its name.
--
underground experts united
https://dataswamp.org/~incal
^ permalink raw reply [flat|nested] 20+ messages in thread
* Re: Lexical vs. dynamic: small examples?
2021-08-14 16:00 ` Eduardo Ochs
@ 2021-08-14 19:41 ` Emanuel Berg via Users list for the GNU Emacs text editor
2021-08-14 20:42 ` tomas
1 sibling, 0 replies; 20+ messages in thread
From: Emanuel Berg via Users list for the GNU Emacs text editor @ 2021-08-14 19:41 UTC (permalink / raw)
To: help-gnu-emacs
Eduardo Ochs wrote:
> The two programming languages that I use more are Elisp and
> Lua, and Lua
They (the Brazilians) did Lua (moon in Portugese) because
Python wasn't around, and Lisp was too cryptic with its
"unfriendly" syntax :) [1]
> the easiest way to understand a specification that can be
> implemented in many ways is to study the specification AND
> one implementation of it.
ikr?
> details [...] of the current implementation of lexical
> binding in Emacs are hard to find, so I'm writing them
> myself
Okay, but how many situations are they?
You already have the example with `let', what other situations
can you think of?
One example per situation should be enough and do them as
short as possible. Because there aren't that many, are there?
[1] https://en.wikipedia.org/wiki/Lua_(programming_language)
--
underground experts united
https://dataswamp.org/~incal
^ permalink raw reply [flat|nested] 20+ messages in thread
* Re: Lexical vs. dynamic: small examples?
2021-08-14 19:00 ` Gregory Heytings
@ 2021-08-14 20:16 ` Emanuel Berg via Users list for the GNU Emacs text editor
2021-08-14 20:23 ` Gregory Heytings
2021-08-14 20:41 ` tomas
1 sibling, 1 reply; 20+ messages in thread
From: Emanuel Berg via Users list for the GNU Emacs text editor @ 2021-08-14 20:16 UTC (permalink / raw)
To: help-gnu-emacs
Gregory Heytings wrote:
> (defun delete-whitespace-at-eol ()
> (interactive)
> (save-excursion (replace-regexp " *$" "" nil (point-min) (point-max))))
OK, that's an example, but let's mention there is
`delete-trailing-whitespace'.
(defun untab-all ()
(unless (member major-mode '(makefile-gmake-mode
makefile-mode) ) ; exceptions
(untabify (point-min) (point-max)))
nil) ; "did not write buffer to disk"
(defun before-save-hook-f ()
(untab-all)
(delete-trailing-whitespace) )
(add-hook 'before-save-hook #'before-save-hook-f)
> Now if you M-x trim-whitespaces-at-eol in a read-only
> buffer, you'll get a "Buffer is read-only" error.
> Suppose you want to make that function work for read-only
> buffers. If Emacs Lisp only had lexical binding, you would
> have to alter the global "buffer-read-only" variable, that
> is, to do something like:
>
> (defvar saved-buffer-read-only)
> (defun delete-whitespace-at-eol ()
> (interactive)
> (setq saved-buffer-read-only buffer-read-only)
> (setq buffer-read-only nil)
> (save-excursion (replace-regexp " *$" "" nil (point-min) (point-max)))
> (setq buffer-read-only saved-buffer-read-only))
OK, but you don't need to store the old value in a global
variable...
> This becomes much simpler with dynamic binding:
>
> (defun delete-whitespace-at-eol ()
> (interactive)
> (let ((buffer-read-only nil))
> (save-excursion (replace-regexp " *$" "" nil (point-min) (point-max)))))
Can't we have two `let', the old let that is lexical/static
and another let that is dynamic, only we call it something
else, and then everyone can use whatever they like and it is
always clear from the code which it is and we can even have
both from the same file, with no need to say it's this way or
the other?
It doesn't work like that? Why not?
--
underground experts united
https://dataswamp.org/~incal
^ permalink raw reply [flat|nested] 20+ messages in thread
* Re: Lexical vs. dynamic: small examples?
2021-08-14 20:16 ` Emanuel Berg via Users list for the GNU Emacs text editor
@ 2021-08-14 20:23 ` Gregory Heytings
2021-08-14 21:05 ` Emanuel Berg via Users list for the GNU Emacs text editor
0 siblings, 1 reply; 20+ messages in thread
From: Gregory Heytings @ 2021-08-14 20:23 UTC (permalink / raw)
To: Emanuel Berg; +Cc: help-gnu-emacs
>> (defvar saved-buffer-read-only)
>> (defun delete-whitespace-at-eol ()
>> (interactive)
>> (setq saved-buffer-read-only buffer-read-only)
>> (setq buffer-read-only nil)
>> (save-excursion (replace-regexp " *$" "" nil (point-min) (point-max)))
>> (setq buffer-read-only saved-buffer-read-only))
>
> OK, but you don't need to store the old value in a global variable...
>
Of course you do, you need to restore the original value, which could be
either t or nil. But indeed TIMTOWTDI: for this specific example, another
way to do it would be to introduce a conditional. In general however
variables can hold more than two values, so for didactic purposes the
above is much clearer.
^ permalink raw reply [flat|nested] 20+ messages in thread
* Re: Lexical vs. dynamic: small examples?
2021-08-14 19:31 ` Emanuel Berg via Users list for the GNU Emacs text editor
@ 2021-08-14 20:31 ` tomas
2021-08-14 21:26 ` Emanuel Berg via Users list for the GNU Emacs text editor
0 siblings, 1 reply; 20+ messages in thread
From: tomas @ 2021-08-14 20:31 UTC (permalink / raw)
To: help-gnu-emacs
[-- Attachment #1: Type: text/plain, Size: 607 bytes --]
On Sat, Aug 14, 2021 at 09:31:20PM +0200, Emanuel Berg via Users list for the GNU Emacs text editor wrote:
> tomas wrote:
>
> > The metaphor which, for me, did "click" was: lexical binding
> > is a binding along "space", dynamic binding along "time".
>
> Okay, but as for dynamic, isn't that the definition rather
> than a metaphor?
If you take it too seriously, you end up with a strange time,
which goes back and forth and branches. Calls stacks like to
do funny things :-/
If you add nonlocal exits (exceptions, setjmp/longjmp, continuations),
you are in hot water ;-)
Cheers
- t
[-- Attachment #2: Digital signature --]
[-- Type: application/pgp-signature, Size: 198 bytes --]
^ permalink raw reply [flat|nested] 20+ messages in thread
* Re: Lexical vs. dynamic: small examples?
2021-08-14 19:00 ` Gregory Heytings
2021-08-14 20:16 ` Emanuel Berg via Users list for the GNU Emacs text editor
@ 2021-08-14 20:41 ` tomas
1 sibling, 0 replies; 20+ messages in thread
From: tomas @ 2021-08-14 20:41 UTC (permalink / raw)
To: help-gnu-emacs
[-- Attachment #1: Type: text/plain, Size: 1234 bytes --]
On Sat, Aug 14, 2021 at 07:00:29PM +0000, Gregory Heytings wrote:
>
> >
> >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...
> >
>
> Suppose you write a function to remove unnecessary whitespaces at
> the end of lines:
>
> (defun delete-whitespace-at-eol ()
> (interactive)
> (save-excursion (replace-regexp " *$" "" nil (point-min) (point-max))))
This is a good example. There are other ones. Basically, whenever
you want some "dynamic state" to be in effect for the whole
"call tree" below you. Typically those are well-known things
with an agreed-upon purpose.
My favourite example is some kind of "debug setting" (a function,
a value, whatever).
There's a reason why the convention in CL is to have those
variable named with asterisks around, like *this*.
(I had many arguments whithin Perl. With the arrival of lexical
variables, there was a fraction which tried to convince people
to /never/ use dynamic variables, and I tried to explain that,
yes, use sparingly, but sometimes they can make code clearer.
Cheers
- t
[-- Attachment #2: Digital signature --]
[-- Type: application/pgp-signature, Size: 198 bytes --]
^ permalink raw reply [flat|nested] 20+ messages in thread
* Re: Lexical vs. dynamic: small examples?
2021-08-14 16:00 ` Eduardo Ochs
2021-08-14 19:41 ` Emanuel Berg via Users list for the GNU Emacs text editor
@ 2021-08-14 20:42 ` tomas
1 sibling, 0 replies; 20+ messages in thread
From: tomas @ 2021-08-14 20:42 UTC (permalink / raw)
To: Eduardo Ochs; +Cc: help-gnu-emacs
[-- Attachment #1: Type: text/plain, Size: 715 bytes --]
On Sat, Aug 14, 2021 at 01:00:34PM -0300, Eduardo Ochs wrote:
> 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.
Hm. I'll have to wrap my head around that.
Cheers
- t
[-- Attachment #2: Digital signature --]
[-- Type: application/pgp-signature, Size: 198 bytes --]
^ permalink raw reply [flat|nested] 20+ messages in thread
* Re: Lexical vs. dynamic: small examples?
2021-08-14 20:23 ` Gregory Heytings
@ 2021-08-14 21:05 ` Emanuel Berg via Users list for the GNU Emacs text editor
2021-08-14 21:13 ` tomas
0 siblings, 1 reply; 20+ messages in thread
From: Emanuel Berg via Users list for the GNU Emacs text editor @ 2021-08-14 21:05 UTC (permalink / raw)
To: help-gnu-emacs
Gregory Heytings wrote:
>>> (defvar saved-buffer-read-only)
>>> (defun delete-whitespace-at-eol ()
>>> (interactive)
>>> (setq saved-buffer-read-only buffer-read-only)
>>> (setq buffer-read-only nil)
>>> (save-excursion (replace-regexp " *$" "" nil (point-min) (point-max)))
>>> (setq buffer-read-only saved-buffer-read-only))
>>
>> OK, but you don't need to store the old value in a global variable...
>>
> Of course you do, you need to restore the original value [...]
But there is still no need to use a global variable...
--
underground experts united
https://dataswamp.org/~incal
^ permalink raw reply [flat|nested] 20+ messages in thread
* Re: Lexical vs. dynamic: small examples?
2021-08-14 21:05 ` Emanuel Berg via Users list for the GNU Emacs text editor
@ 2021-08-14 21:13 ` tomas
2021-08-14 21:28 ` Emanuel Berg via Users list for the GNU Emacs text editor
0 siblings, 1 reply; 20+ messages in thread
From: tomas @ 2021-08-14 21:13 UTC (permalink / raw)
To: help-gnu-emacs
[-- Attachment #1: Type: text/plain, Size: 827 bytes --]
On Sat, Aug 14, 2021 at 11:05:53PM +0200, Emanuel Berg via Users list for the GNU Emacs text editor wrote:
> Gregory Heytings wrote:
>
> >>> (defvar saved-buffer-read-only)
> >>> (defun delete-whitespace-at-eol ()
> >>> (interactive)
> >>> (setq saved-buffer-read-only buffer-read-only)
> >>> (setq buffer-read-only nil)
> >>> (save-excursion (replace-regexp " *$" "" nil (point-min) (point-max)))
> >>> (setq buffer-read-only saved-buffer-read-only))
> >>
> >> OK, but you don't need to store the old value in a global variable...
> >>
> > Of course you do, you need to restore the original value [...]
>
> But there is still no need to use a global variable...
That's not the point. You perhaps don't want to trample on one. The
let-binding helps you with that. In both cases.
Cheers
- t
[-- Attachment #2: Digital signature --]
[-- Type: application/pgp-signature, Size: 198 bytes --]
^ permalink raw reply [flat|nested] 20+ messages in thread
* Re: Lexical vs. dynamic: small examples?
2021-08-14 20:31 ` tomas
@ 2021-08-14 21:26 ` Emanuel Berg via Users list for the GNU Emacs text editor
2021-08-14 21:29 ` Emanuel Berg via Users list for the GNU Emacs text editor
0 siblings, 1 reply; 20+ messages in thread
From: Emanuel Berg via Users list for the GNU Emacs text editor @ 2021-08-14 21:26 UTC (permalink / raw)
To: help-gnu-emacs
tomas wrote:
> If you add nonlocal exits (exceptions, setjmp/longjmp,
> continuations), you are in hot water ;-)
Let's see, an exception is an exception...
setjmp/longjmp are C functions to save state and then
restore/resume execution at a specific location after
an error.
A continuation - the functional equivalent of the GOTO
statement <https://en.wikipedia.org/wiki/Continuation> - is
used in continuation-passing style computing, CPS, where an
explicit continuation function is sent to a function and
that gets called with the value instead of returning it.
So if f(a, f') = f'(f(a)) then f' is the continuation?
"Nonlocal exits unbind all variable bindings made by the
constructs being exited."
<https://www.gnu.org/software/emacs/manual/html_node/elisp/Nonlocal-Exits.html>
--
underground experts united
https://dataswamp.org/~incal
^ permalink raw reply [flat|nested] 20+ messages in thread
* Re: Lexical vs. dynamic: small examples?
2021-08-14 21:13 ` tomas
@ 2021-08-14 21:28 ` Emanuel Berg via Users list for the GNU Emacs text editor
0 siblings, 0 replies; 20+ messages in thread
From: Emanuel Berg via Users list for the GNU Emacs text editor @ 2021-08-14 21:28 UTC (permalink / raw)
To: help-gnu-emacs
tomas wrote:
>> But there is still no need to use a global variable...
>
> That's not the point.
It is my point.
--
underground experts united
https://dataswamp.org/~incal
^ permalink raw reply [flat|nested] 20+ messages in thread
* Re: Lexical vs. dynamic: small examples?
2021-08-14 21:26 ` Emanuel Berg via Users list for the GNU Emacs text editor
@ 2021-08-14 21:29 ` Emanuel Berg via Users list for the GNU Emacs text editor
0 siblings, 0 replies; 20+ messages in thread
From: Emanuel Berg via Users list for the GNU Emacs text editor @ 2021-08-14 21:29 UTC (permalink / raw)
To: help-gnu-emacs
> <https://www.gnu.org/software/emacs/manual/html_node/elisp/Nonlocal-Exits.html>
That page doesn't have a title BTW.
--
underground experts united
https://dataswamp.org/~incal
^ permalink raw reply [flat|nested] 20+ messages in thread
* Re: Lexical vs. dynamic: small examples?
@ 2022-01-09 17:40 Drew Adams
2022-01-10 13:10 ` Emanuel Berg via Users list for the GNU Emacs text editor
0 siblings, 1 reply; 20+ messages in thread
From: Drew Adams @ 2022-01-09 17:40 UTC (permalink / raw)
To: Emanuel Berg, 'Help-Gnu-Emacs (help-gnu-emacs@gnu.org)'
[-- Attachment #1: Type: text/plain, Size: 1776 bytes --]
> >> FWIW, I just wrote a tiny library, `dyna-show.el', that
> >> defines minor mode `dyna-show-mode', which highlights
> >> occurrences of "special" (aka dynamically
> >> scoped) variables.
> >>
> >> It just uses `special-variable-p', which is limited.
> >>
> >> From the attachment, you can see another limitation:
> >> occurrences of a function, e.g. `font-lock-mode', that has
> >> the same name as a dynamic variable are also highlighted.
> >
> > Thanks, it is very helpful for cosmetics of the code.
>
> But here it also has a functional value. Or to be even more
> precise, the functional value is also what is appealing, since
> I think it started in that end.
tl;dr:
1. It can be useful. 2. It's not foolproof.
___
I guess you're referring to this (from the
`dyna-show.el' Commentary)?
[I]f a function has the same name as a dynamic
variable, then its occurrences are also
highlighted, as if they were occurrences of
the variable.
For example `font-lock-mode' is a variable as
well as a function. Both kinds of occurrences
of that symbol are highlighted the same.
Whether this is considered a feature or a
limitation, the reason is that it requires no
analysis of the code (which would anyway be
problematic and limited) to determine how each
occurrence is used.
The Commentary calls it out as a limitation.
And the sentence above comes right after this
additional caveat:
The simple built-in test `special-variable-p'
is used. That test is not 100% reliable. It
doesn't respect vacuous `defvar' sexps, which
declare a variable to be special in a given
context, without assigning a value to the
variable. Instead, it uses `defvar',`defconst',
and `defcustom' sexps with a value arg present.
[-- Attachment #2: winmail.dat --]
[-- Type: application/ms-tnef, Size: 13769 bytes --]
^ permalink raw reply [flat|nested] 20+ messages in thread
* Re: Lexical vs. dynamic: small examples?
2022-01-09 17:40 Lexical vs. dynamic: small examples? Drew Adams
@ 2022-01-10 13:10 ` Emanuel Berg via Users list for the GNU Emacs text editor
0 siblings, 0 replies; 20+ messages in thread
From: Emanuel Berg via Users list for the GNU Emacs text editor @ 2022-01-10 13:10 UTC (permalink / raw)
To: help-gnu-emacs
Drew Adams wrote:
>> But here it also has a functional value. Or to be even more
>> precise, the functional value is also what is appealing,
>> since I think it started in that end.
>
> tl;dr:
> 1. It can be useful. 2. It's not foolproof.
> ___
>
> I guess you're referring to this (from the
> `dyna-show.el' Commentary)?
>
> [I]f a function has the same name as a dynamic
> variable, then its occurrences are also
> highlighted, as if they were occurrences of
> the variable.
>
> For example `font-lock-mode' is a variable as
> well as a function. Both kinds of occurrences
> of that symbol are highlighted the same.
>
> Whether this is considered a feature or a
> limitation, the reason is that it requires no
> analysis of the code (which would anyway be
> problematic and limited) to determine how each
> occurrence is used.
>
> The Commentary calls it out as a limitation.
> And the sentence above comes right after this
> additional caveat:
>
> The simple built-in test `special-variable-p' is used.
> That test is not 100% reliable. It doesn't respect vacuous
> `defvar' sexps, which declare a variable to be special in
> a given context, without assigning a value to the
> variable. Instead, it uses `defvar',`defconst', and
> `defcustom' sexps with a value arg present.
Well, yes, that's a good example, but actually the functional
gain starts with the programmer thinking "hey, what should it
be called?" - "obey tradition" (follow the convention) and
even at that point the harsh reality of a programmer gets
a little "bit" easier ;)
--
underground experts united
https://dataswamp.org/~incal
^ permalink raw reply [flat|nested] 20+ messages in thread
end of thread, other threads:[~2022-01-10 13:10 UTC | newest]
Thread overview: 20+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2022-01-09 17:40 Lexical vs. dynamic: small examples? Drew Adams
2022-01-10 13:10 ` Emanuel Berg via Users list for the GNU Emacs text editor
-- strict thread matches above, loose matches on Subject: below --
2021-08-14 3:34 Eduardo Ochs
2021-08-14 3:56 ` Emanuel Berg via Users list for the GNU Emacs text editor
2021-08-14 4:12 ` Eduardo Ochs
2021-08-14 7:35 ` tomas
2021-08-14 16:00 ` Eduardo Ochs
2021-08-14 19:41 ` Emanuel Berg via Users list for the GNU Emacs text editor
2021-08-14 20:42 ` tomas
2021-08-14 19:31 ` Emanuel Berg via Users list for the GNU Emacs text editor
2021-08-14 20:31 ` tomas
2021-08-14 21:26 ` Emanuel Berg via Users list for the GNU Emacs text editor
2021-08-14 21:29 ` Emanuel Berg via Users list for the GNU Emacs text editor
2021-08-14 19:00 ` Gregory Heytings
2021-08-14 20:16 ` Emanuel Berg via Users list for the GNU Emacs text editor
2021-08-14 20:23 ` Gregory Heytings
2021-08-14 21:05 ` Emanuel Berg via Users list for the GNU Emacs text editor
2021-08-14 21:13 ` tomas
2021-08-14 21:28 ` Emanuel Berg via Users list for the GNU Emacs text editor
2021-08-14 20:41 ` tomas
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).