Hi all, it's been quite some time since lexical binding was introduced to Elisp. Apart from possible optimizations and being less error-prone, it allows for all the cool tricks with closures. Are there any places in Emacs core where "proper" closures are actually used? (By "proper", I mean closures with non-empty lexical environment, actually using said environment.) TIA, -- Marcin Borkowski http://mbork.pl
[-- Attachment #1: Type: text/plain, Size: 723 bytes --] On Tue, Jan 12, 2021 at 02:50:24PM +0100, Marcin Borkowski wrote: > Hi all, > > it's been quite some time since lexical binding was introduced to > Elisp. Apart from possible optimizations and being less error-prone, it > allows for all the cool tricks with closures. Are there any places in > Emacs core where "proper" closures are actually used? (By "proper", > I mean closures with non-empty lexical environment, actually using said > environment.) tar-next-file-displayer of tar-mode.el seems to do (CAVEAT: I just grepped for "closure" in the .el sources, dismissed the too-obvious false-positives and half-scanned comments and code, so I might be off by some amount! Double-check.) Cheers - t [-- Attachment #2: Digital signature --] [-- Type: application/pgp-signature, Size: 198 bytes --]
[-- Attachment #1: Type: text/plain, Size: 575 bytes --] Marcin Borkowski writes: > Hi all, > > it's been quite some time since lexical binding was introduced to > Elisp. Apart from possible optimizations and being less error-prone, it > allows for all the cool tricks with closures. Are there any places in > Emacs core where "proper" closures are actually used? (By "proper", > I mean closures with non-empty lexical environment, actually using said > environment.) > > TIA, thunk.el & generator.el -- Retrieve my PGP public key: gpg --recv-keys D47A9C8B2AE3905B563D9135BE42B352A9F6821F Zihao [-- Attachment #2: signature.asc --] [-- Type: application/pgp-signature, Size: 255 bytes --]
> it's been quite some time since lexical binding was introduced to
> Elisp. Apart from possible optimizations and being less error-prone, it
> allows for all the cool tricks with closures. Are there any places in
> Emacs core where "proper" closures are actually used? (By "proper",
> I mean closures with non-empty lexical environment, actually using said
> environment.)
Yes. Of course, there are lots of "proper" closures used at places
where the old code used dynamic scoping instead, such as:
(mapcar (lambda (x) (concat x y)) foo)
I.e. using "downward funargs". But there are also several places where
we use proper closures that survive their enclosing scope.
The completion-table functions do that a fair bit, for example, as does
`cl-generic`. `gv-ref` is another example, along with `thunk`.
Several process filters and sentinels do that as well.
And then there are all the places where we replaced
`(lambda ...)
with proper closures.
[ And then, there's `add-function` which relies on "proper closures" but
it does it by manually constructing the bytecode objects rather than by
relying on the `lexical-binding` support for lexical scope, so it's
somewhat different (it's more like the use of `(lambda ...), just with
byte-code objects). ]
Stefan
Marcin Borkowski <mbork@mbork.pl> writes:
> Hi all,
>
> it's been quite some time since lexical binding was introduced to
> Elisp. Apart from possible optimizations and being less error-prone, it
> allows for all the cool tricks with closures. Are there any places in
> Emacs core where "proper" closures are actually used? (By "proper",
> I mean closures with non-empty lexical environment, actually using said
> environment.)
I've put a print in `byte-compile-make-closure' and did a bootstrap.
I'm counting 7289 occurrences in the Emacs code base. Is this a correct
estimation?
Andrea
> I've put a print in `byte-compile-make-closure' and did a bootstrap.
>
> I'm counting 7289 occurrences in the Emacs code base. Is this a correct
> estimation?
Probably, but I suspect most of them are in the "downward funarg"
category, i.e. they'd work just as well with dynamic scope because they
don't outlive their own scope.
Stefan