[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Returning variable "references" under lexical binding
From: |
Sean McAfee |
Subject: |
Returning variable "references" under lexical binding |
Date: |
Mon, 20 May 2013 13:35:26 -0700 |
User-agent: |
Gnus/5.13 (Gnus v5.13) Emacs/24.1 (gnu/linux) |
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?
- Returning variable "references" under lexical binding,
Sean McAfee <=
- 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, 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