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

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

Re: Closures in Emacs and their usage scenarios.


From: Stefan Monnier
Subject: Re: Closures in Emacs and their usage scenarios.
Date: Wed, 29 Sep 2021 08:28:10 -0400
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/28.0.50 (gnu/linux)

Tomas Hlavaty [2021-09-29 08:10:36] wrote:
> On Wed 29 Sep 2021 at 00:04, Stefan Monnier via Users list for the GNU Emacs
> text editor <help-gnu-emacs@gnu.org> wrote:
>> And captured variables that are mutated are also quite rare
> I don't think so.

How rare can depend on the language (as mentioned: they are completely
absent from Haskell/OCaml and friends), but in my experience in ELisp
they're the exception rather than the rule and IIUC it's the same in Scheme.

Note also that such mutated+captured variables are more costly (at
least in the most prevalent way to implement closures).  E.g. in ELisp,
a code like

    (let ((x 0))
      (lambda () (setq x (1+ x))))

will be turned into

    (let ((x' (list 0)))
      (lambda () (setcar x' (1+ (car x')))))

[ With more work we could avoid such a rewrite in the above case, but
  it's basically unavoidable in more complex cases like:

    (let ((x 0))
      (list
        (lambda () (setq x (1+ x)))
        (lambda () x)))

  Note that the same kind of rewrite is used in many/most Scheme
  implementations.  ]

> The best thing about closures is that they allow
> wrapping state mutations under simple interface of funcall.

You may be fond of that feature but closures are very valuable even
without it.


        Stefan




reply via email to

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