[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Returning variable "references" under lexical binding
From: |
Barry Margolin |
Subject: |
Re: Returning variable "references" under lexical binding |
Date: |
Mon, 20 May 2013 21:18:30 -0400 |
User-agent: |
MT-NewsWatcher/3.5.3b3 (Intel Mac OS X) |
In article <bp8sj1hjsoh.fsf@usca1uw-JZWWPM1.sanmateo.corp.akamai.com>,
Sean McAfee <eefacm@gmail.com> wrote:
> I recently tried writing my first non-toy code that employs lexical
> binding. It's a routine that sets up a series of idle timers, storing
> each successive timer object into the same lexical variable.
>
> I want my routine to return an object that can be used to cancel the
> most recently set timer. If I were writing this code in the days prior
> to lexical binding, I might have dono something like this:
>
> (defun start-my-timer ()
> (let ((timer (gensym)))
> ;; ... (set timer (make-timer ...)) ...
> timer))
>
> (defun cancel-my-timer (timer)
> (cancel-timer (symbol-value timer)))
>
> The documenentation for lexical variables cautions against treating them
> as symbols, specifically stating that functions like symbol-value will
> not work. So I wrote my routine to return a closure:
>
> (defun start-my-timer ()
> (let (timer)
> ;; ... (setq timer (make-timer ...)) ...
> (lambda () timer)))
>
> (defun cancel-my-timer (timer)
> (cancel-timer (funcall timer)))
>
> This works, but is it the "correct" way to do this?
What's wrong with using ordinary variable values:
(defun start-my-timer ()
(make-timer ...))
(setq my-timer (start-my-timer))
(cancel-timer my-timer)
What do you gain by indirecting through a gensym or a closure?
--
Barry Margolin, barmar@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
- Returning variable "references" under lexical binding, Sean McAfee, 2013/05/20
- Re: Returning variable "references" under lexical binding,
Barry Margolin <=
- Re: Returning variable "references" under lexical binding, Stefan Monnier, 2013/05/20
- Message not available
- Re: Returning variable "references" under lexical binding, Sean McAfee, 2013/05/21
- Re: Returning variable "references" under lexical binding, Stefan Monnier, 2013/05/21
- Re: Returning variable "references" under lexical binding, Barry Margolin, 2013/05/21
- Re: Returning variable "references" under lexical binding, Sean McAfee, 2013/05/21
- Re: Returning variable "references" under lexical binding, Barry Margolin, 2013/05/21
- Re: Returning variable "references" under lexical binding, Sean McAfee, 2013/05/21
- Re: Returning variable "references" under lexical binding, Stefan Monnier, 2013/05/22