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
Subject: Re: Timer variable binding
Date: Tue, 07 Jan 2014 17:15:16 +0100	[thread overview]
Message-ID: <87d2k3ydij.fsf@yahoo.fr> (raw)
In-Reply-To: <CAB6RKMt9qVAsejgL6CubDYQmk9RvxKMfrxwkJuYpe0LHWcpsmQ@mail.gmail.com> (Johan Andersson's message of "Tue, 7 Jan 2014 15:53:25 +0100")

Johan Andersson <johan.rejeep@gmail.com> writes:
> I have some questions regarding timers.

To understand what's going on, you also need to understand what lexical
and dynamical binding is. See (info "(elisp) Variable Scoping")

> This code will start a timer and the first time the callback runs, the
> timer is canceled. Works great!
>
> (let ((timer (run-at-time 0 1 (lambda ()
>                                 (cancel-timer timer))))))
>
> In the above example I can access the timer variable inside the function
> callback.

Just changing the name from "timer" to "foobar" will make it complain :

(let ((foobar (run-at-time 1 1 (lambda ()
                                (cancel-timer foobar))))))

In fact you are lucky (or not, depending on the point of view) to give
your variable a symbol (namely, "timer") that is dynamically bound to
the 'current timer' at the time the callback function is being run.

I guess it happens in the function "timer-event-handler" : the argument
of that function is named "timer", and it is dynamically bound because
that file does not use lexical binding -- if it did, it'd make an error
with "timer" as well as with "foobar".

> But in this code, I cannot access the variable my-var. Nothing is
> printed. In Emacs 24.3.1 I see no error, but in 24.3.50.1 I get
> (void-variable my-var). Why is there no error in 24.3.1?

I don't know.

> (let* (timer (my-var 10))
>   (setq timer (run-at-time 0 1 (lambda ()
>                                  (print my-var)
>                                  (cancel-timer timer)))))

This now should make sense : in fact none of your "timer" or "my-var"
are seen by the callback. It's pure luck that "timer", when the lambda
is run, refers to the current timer.

Now if you run your code with lexical-binding set to 't'
(let* (timer (my-var 10))
  (setq timer (run-at-time 0 1 (lambda ()
                                 (print my-var)
                                 (cancel-timer timer)))))
It'll work as expected: the lambda now is made into a closure (i.e. a
function which knows about its current lexical environment).

Then this will work :
(let* (timer (my-var 10))
  (setq timer (run-at-time 0 1 (lambda nil
                                 (setq my-var (1- my-var))
                                 (message "my-var: %s" my-var)
                                 (when (= my-var 0)
                                   (cancel-timer timer))))))

but this won't work :

(let* (timer (my-var 10))
  (setq timer (run-at-time 0 1 (lambda (my-var)
                                 (setq my-var (1- my-var))
                                 (message "my-var: %s" my-var)
                                 (when (= my-var 0)
                                   (cancel-timer timer)))
                           my-var)))

> Can someone please explain these weird behaviors?

I hope I did ; don't hesitate to ask further.

-- 
Nicolas



  reply	other threads:[~2014-01-07 16:15 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 [this message]
2014-01-07 19:52   ` Johan Andersson
2014-01-07 21:57     ` Nicolas Richard
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=87d2k3ydij.fsf@yahoo.fr \
    --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.