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

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

Re: Lexical binding doesn't seem to be faster?


From: tomas
Subject: Re: Lexical binding doesn't seem to be faster?
Date: Sat, 9 Mar 2019 09:21:29 +0100
User-agent: Mutt/1.5.21 (2010-09-15)

On Fri, Mar 08, 2019 at 08:53:32AM -0500, Stefan Monnier wrote:

[free variables, lexical vs. dynamic bindings and differences in
 behaviour]

> Actually, in most such cases the code behaves identically in the end,
> but it gets there in a different way (so yes, in some cases it does
> behave differently).
> 
> E.g.
> 
>     (let ((buf (current-buffer)))
>       [...]
>       (with-temp-buffer
>         (mapcar (lambda (x)
>                   (with-current-buffer buf ...))
>                 ...))
>       [...])
>       
> The end result will most likely be the same regardless which binding
> style is used, but the way to find the value of `buf` from within the
> lambda is different in the two cases.
> 
> The difference becomes apparent if you do
> 
>     (advice-add 'mapcar :around
>                 (lambda (&rest orig-call)
>                   (let ((buf 42)) (apply orig-call))))
>                   
> since in this case dynamic-binding will cause your `with-current-buffer`
> to try and use buffer 42 and signal an error (because of the name
> conflict, aka "variable capture"), whereas lexical-binding
> will be unaffected.

Thanks for the nice example. In a way it is somewhat surprising how
"similarly" lexical and dynamic binding tend to behave in general,
but that is, I think, due to how we tend to structure code, avoiding
variables whose "resolution" lies "far away".

And your example illustrates that the differences manifest themselves
most with constructs which upset the "dynamic flow" of the code, i.e.
some kind of callback (which, in a very handwavy way, advice is a kind
of).

I.e. I'm "here" and tell the code "there" to run my own snippet "here"
in its context "there". Or something.

Cheers
-- t

Attachment: signature.asc
Description: Digital signature


reply via email to

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