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

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

Re: Timer variable binding


From: Nicolas Richard
Subject: Re: Timer variable binding
Date: Tue, 7 Jan 2014 21:57:31 +0000 (GMT)

> 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 via email to

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