all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Nicolas Richard <theonewiththeevillook@yahoo.fr>
To: Johan Andersson <johan.rejeep@gmail.com>
Cc: "help-gnu-emacs@gnu.org" <help-gnu-emacs@gnu.org>
Subject: Re: Timer variable binding
Date: Tue, 7 Jan 2014 21:57:31 +0000 (GMT)	[thread overview]
Message-ID: <1389131851.70949.YahooMailNeo@web172805.mail.ir2.yahoo.com> (raw)
In-Reply-To: <CAB6RKMsf_SBWKyyvTO5ZyMhnEajeHGM15mpxGQ4Y=Xp0_B0HWQ@mail.gmail.com>

> 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-07 21:57 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 [this message]
2014-01-08  6:25       ` Johan Andersson

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=1389131851.70949.YahooMailNeo@web172805.mail.ir2.yahoo.com \
    --to=theonewiththeevillook@yahoo.fr \
    --cc=help-gnu-emacs@gnu.org \
    --cc=johan.rejeep@gmail.com \
    /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.