all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Sean McAfee <eefacm@gmail.com>
To: help-gnu-emacs@gnu.org
Subject: Re: Returning variable "references" under lexical binding
Date: Mon, 20 May 2013 22:39:48 -0700	[thread overview]
Message-ID: <87fvxgc2mz.fsf@gmail.com> (raw)
In-Reply-To: mailman.112.1369106352.22516.help-gnu-emacs@gnu.org

Stefan Monnier <monnier@iro.umontreal.ca> writes:
>> (defun start-my-timer ()
>>   (let ((timer (gensym)))
>>     ;; ... (set timer (make-timer ...)) ...
>>     timer))
> [...]
>> (defun cancel-my-timer (timer)
>>   (cancel-timer (symbol-value timer)))

> Why not
>
>   (defun start-my-timer ()
>     (let ((timer (make-timer ...))
>       ...
>       timer))
>   (defun cancel-my-timer (timer)
>     (cancel-timer timer))

Because start-my-timer sets up callbacks that may repeatedly change the
value of the "timer" variable:

;; -*- lexical-binding: t; -*-

(defun start-my-timer ()
  (let (timer)
    (setq timer (make-timer ...))
    ;; ... (later (occasionally (setq timer (make-timer ...)))) ...
    (lambda () timer))

Like I said in my original article:

> It's a routine that sets up a series of idle timers, storing
> each successive timer object into the same lexical variable.

I should have made that more explicit.

>> The documenentation for lexical variables cautions against treating them
>> as symbols, specifically stating that functions like symbol-value will
>> not work.

> In your above code, you're not treating variables as symbols.
> You're just storing a symbol inside a variable, which is fine.

Yes, I was explaining why I assumed this wouldn't work:

;; -*- lexical-binding: t; -*-

(defun start-my-timer ()
  (let (timer)
    (setq timer (make-timer ...))
    ;; ... (later (occasionally (setq timer (make-timer ...)))) ...
    'timer)

(defun cancel-timer (timer)
  (cancel-timer (symbol-value timer)))

If I were not using lexical binding, though, this should work:

(defun start-my-timer ()
  (let ((timer (gensym)))
    (set timer (make-timer ...))
    ;; ... (later (occasionally (set timer (make-timer ...)))) ...
    timer))

;; cancel-timer as before

The question still stands if using a closure as a handle to a varying
lexical variable is the right way to go.


  parent reply	other threads:[~2013-05-21  5:39 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-05-20 20:35 Returning variable "references" under lexical binding Sean McAfee
2013-05-21  1:18 ` Barry Margolin
2013-05-21  3:18 ` Stefan Monnier
     [not found] ` <mailman.112.1369106352.22516.help-gnu-emacs@gnu.org>
2013-05-21  5:39   ` Sean McAfee [this message]
2013-05-21 12:54     ` Stefan Monnier
2013-05-21 14:23     ` Barry Margolin
2013-05-21 16:38       ` Sean McAfee
2013-05-21 18:06         ` Barry Margolin
2013-05-21 22:43           ` Sean McAfee
2013-05-23  1:01             ` Stefan Monnier
  -- strict thread matches above, loose matches on Subject: below --
2013-05-21 14:41 Barry OReilly

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=87fvxgc2mz.fsf@gmail.com \
    --to=eefacm@gmail.com \
    --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.