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 22:41:04 +0200
User-agent: Mutt/1.5.21 (2010-09-15)

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: signature.asc
Description: Digital signature


reply via email to

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