From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Sean McAfee Newsgroups: gmane.emacs.help Subject: Re: Returning variable "references" under lexical binding Date: Mon, 20 May 2013 22:39:48 -0700 Organization: A noiseless patient Spider Message-ID: <87fvxgc2mz.fsf@gmail.com> References: NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: text/plain X-Trace: ger.gmane.org 1369118859 15939 80.91.229.3 (21 May 2013 06:47:39 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Tue, 21 May 2013 06:47:39 +0000 (UTC) To: help-gnu-emacs@gnu.org Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Tue May 21 08:47:39 2013 Return-path: Envelope-to: geh-help-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by plane.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1UegMC-0005tw-E9 for geh-help-gnu-emacs@m.gmane.org; Tue, 21 May 2013 08:47:36 +0200 Original-Received: from localhost ([::1]:43317 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1UegMC-0002x4-2c for geh-help-gnu-emacs@m.gmane.org; Tue, 21 May 2013 02:47:36 -0400 Original-Path: usenet.stanford.edu!goblin1!goblin.stu.neva.ru!eternal-september.org!feeder.eternal-september.org!mx05.eternal-september.org!.POSTED!not-for-mail Original-Newsgroups: gnu.emacs.help Original-Lines: 68 Injection-Info: mx05.eternal-september.org; posting-host="f324fc4e4d4ac5fe7ce88243f913f9a8"; logging-data="8651"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/rMYtng0yNUoK/Hw1Vt/2CjDxrn0gORT8=" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.1 (gnu/linux) Cancel-Lock: sha1:5wVCBmtREvjPbd4KN+GnMVB7WS4= sha1:5MTd3DBZFlra1BeqnZjNNc5GeF0= Original-Xref: usenet.stanford.edu gnu.emacs.help:198671 X-Mailman-Approved-At: Tue, 21 May 2013 02:47:26 -0400 X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Original-Sender: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.help:90936 Archived-At: Stefan Monnier 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.