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

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

Re: [External] : How to create a higher order function?


From: Marcin Borkowski
Subject: Re: [External] : How to create a higher order function?
Date: Tue, 05 Oct 2021 09:25:52 +0200
User-agent: mu4e 1.1.0; emacs 28.0.50

On 2021-09-29, at 06:28, Emanuel Berg via Users list for the GNU Emacs text 
editor <help-gnu-emacs@gnu.org> wrote:

> Marcin Borkowski wrote:
>
>> Try this code. Make sure you set `lexical-binding' to t,
>> e.g., by saving it to a file and visiting it again
>> or something.
>>
>> ;;; -*- lexical-binding: t; -*-
>>
>> (setq global-lexical 24)
>>
>> (defun lexical-test ()
>>   (message "%s" global-lexical))
>>
>> (setq global-lexical 17)
>>
>> (lexical-test)
>>
>> (let ((global-lexical 12))
>>   (lexical-test))
>>
>> (defvar global-dynamic 24)
>>
>> (defun dynamic-test ()
>>   (message "%s" global-dynamic))
>>
>> (dynamic-test)
>>
>> (setq global-dynamic 17)
>>
>> (dynamic-test)
>>
>> (let ((global-dynamic 12))
>>   (dynamic-test))
>>
>> Can you spot the difference? Can you see why the dynamic one
>> is better suited for options, i.e., you can temporarily set
>> it with `let' and it Just Works?
>
> You mean options not from a human user, but an Elisp program
> perspective? I.e., different subsystems use other subsystems
> with their own set of options?

Not sure if I understand.

By an "option" I mean a variable I (the user) can set, either via `setq'
(or customize) or via `let' (in my code) so that Emacs behavior is
changed (temporarily in the case of `let').

> I guess that is/would be cool, in particular if it would work
> in parallel ...
>
> Other than that, what I can see, you have provided the same
> old example we have seen many times in slightly different
> versions. It shows that lexical binding is less confusing and
> less error-prone. It also shows the confusion that arises from
> `let' because it not only behaves differently under lexical
> binding and dynamic binding, it also behaves differently with
> lexically and dynamically bound individual variables ...

I wouldn't say "less confusing" - both lexical and dynamical binding
serve their purposes, which are different.

> Split up the `let' is what I think, into slet, `dlet' (which
> already exists), and llet, an alias to `let' (or the other way
> around, doesn't matter), which would be the same as our `let'
> under lexical binding. Then remove all ugly preprocessor-style
> ;;; -*- lexical-binding: t; -*- and everyone can just use
> whatever they like with the different lets having their (and
> only their) behavior in the docstrings, and that behavior
> wouldn't change no matter any outside fiddling around.

You mean you could use the same variable sometimes with `slet',
sometimes with `dlet'?  I'm not sure if that is easy or difficult to
implement.

Best,

-- 
Marcin Borkowski
http://mbork.pl



reply via email to

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