all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: <tomas@tuxteam.de>
To: help-gnu-emacs@gnu.org
Subject: Re: Lexical binding doesn't seem to be faster?
Date: Sat, 9 Mar 2019 09:21:29 +0100	[thread overview]
Message-ID: <20190309082129.GA31605@tuxteam.de> (raw)
In-Reply-To: <jwv36nxbhxv.fsf-monnier+emacs@gnu.org>

[-- Attachment #1: Type: text/plain, Size: 1781 bytes --]

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 #2: Digital signature --]
[-- Type: application/pgp-signature, Size: 198 bytes --]

  reply	other threads:[~2019-03-09  8:21 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-03-06 23:36 Lexical binding doesn't seem to be faster? Hi-Angel
2019-03-08  5:39 ` Stefan Monnier
2019-03-08  8:30   ` tomas
2019-03-08 13:53     ` Stefan Monnier
2019-03-09  8:21       ` tomas [this message]
2019-03-08 13:33   ` Hi-Angel
2019-03-09 15:06     ` Stefan Monnier
2019-03-10 15:16       ` Hi-Angel
2019-03-10 16:14         ` Hi-Angel
2019-03-10 18:07         ` Stefan Monnier
2019-03-10 18:47           ` Hi-Angel
2019-03-10 18:59             ` Stefan Monnier
2019-03-10 19:53               ` Hi-Angel
2019-03-10 20:48                 ` Stefan Monnier
2019-03-10 21:22                   ` Hi-Angel
2019-03-11 19:16                     ` Hi-Angel
2019-03-11 19:26                       ` Stefan Monnier

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20190309082129.GA31605@tuxteam.de \
    --to=tomas@tuxteam.de \
    --cc=help-gnu-emacs@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.