all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Johan Andersson <johan.rejeep@gmail.com>
To: Nicolas Richard <theonewiththeevillook@yahoo.fr>
Cc: help-gnu-emacs@gnu.org
Subject: Re: Timer variable binding
Date: Wed, 8 Jan 2014 07:25:51 +0100	[thread overview]
Message-ID: <CAB6RKMt+w3xwM2HirLcYrWxuO3W=CJ6nFXQ=TSS3SXhbTEhg_A@mail.gmail.com> (raw)
In-Reply-To: <1389131851.70949.YahooMailNeo@web172805.mail.ir2.yahoo.com>

Of course. Obvious now that you say it. Lexical binding it is. Thanks very
much for the answers!
On Jan 7, 2014 10:57 PM, "Nicolas Richard" <theonewiththeevillook@yahoo.fr>
wrote:

> > let (my-var=10) -> run-at-time -> *magic* -> callback-function
> > Looks to me like this should work? But why doesn't it? ;)
>
> By the time the callback is called, the let form is long gone. The
> callback-function is stored in some place (namely, in a timer structure in
> the global variable timer-list) and then, after some time, another piece of
> emacs code calls that function. At that moment, there's no more let binding.
>
> Why it works with lexical binding is because the lambda is made into a
> closure, which then knows what the symbol my-var is.
>
> In a buffer where lexical-binding is t, you can try evalling
> (setq foobar (let ((my-var 0)) (lambda () (incf my-var) (message "my-var:
> %s" my-var))))
> the answer will be:
> (closure ((my-var . 0) t) nil (setq my-var (1+ my-var)) (message "my-var:
> %s" my-var))
> if you now call (funcall foobar), you'll get "my-var: 1"
> and now, evalling foobar gives:
> (closure ((my-var . 1) t) nil (setq my-var (1+ my-var)) (message "my-var:
> %s" my-var))
> This is magic !
>
> Doing the same without lex-bind will give you a lambda, then an error,
> then the same lambda.
>
> --
>
> Nico.
>
>


      reply	other threads:[~2014-01-08  6:25 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-01-07 14:53 Timer variable binding Johan Andersson
2014-01-07 16:15 ` Nicolas Richard
2014-01-07 19:52   ` Johan Andersson
2014-01-07 21:57     ` Nicolas Richard
2014-01-08  6:25       ` Johan Andersson [this message]

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='CAB6RKMt+w3xwM2HirLcYrWxuO3W=CJ6nFXQ=TSS3SXhbTEhg_A@mail.gmail.com' \
    --to=johan.rejeep@gmail.com \
    --cc=help-gnu-emacs@gnu.org \
    --cc=theonewiththeevillook@yahoo.fr \
    /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.