[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Returning variable "references" under lexical binding
From: |
Sean McAfee |
Subject: |
Re: Returning variable "references" under lexical binding |
Date: |
Mon, 20 May 2013 22:39:48 -0700 |
User-agent: |
Gnus/5.13 (Gnus v5.13) Emacs/24.1 (gnu/linux) |
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.
- Returning variable "references" under lexical binding, Sean McAfee, 2013/05/20
- Re: Returning variable "references" under lexical binding, Barry Margolin, 2013/05/20
- Re: Returning variable "references" under lexical binding, Stefan Monnier, 2013/05/20
- Message not available
- Re: Returning variable "references" under lexical binding,
Sean McAfee <=
- 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