From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Barry Margolin Newsgroups: gmane.emacs.help Subject: Re: Returning variable "references" under lexical binding Date: Mon, 20 May 2013 21:18:30 -0400 Organization: A noiseless patient Spider Message-ID: References: NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 X-Trace: ger.gmane.org 1369101519 29441 80.91.229.3 (21 May 2013 01:58:39 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Tue, 21 May 2013 01:58: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 03:58:40 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 1UebqY-00087q-6X for geh-help-gnu-emacs@m.gmane.org; Tue, 21 May 2013 03:58:38 +0200 Original-Received: from localhost ([::1]:60909 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1UebqX-0005OU-Sg for geh-help-gnu-emacs@m.gmane.org; Mon, 20 May 2013 21:58:37 -0400 X-Received: by 10.180.76.115 with SMTP id j19mr1690187wiw.2.1369099110690; Mon, 20 May 2013 18:18:30 -0700 (PDT) Original-Path: usenet.stanford.edu!lg1no21106698wic.0!news-out.google.com!ed6ni6849wic.0!nntp.google.com!proxad.net!feeder1-2.proxad.net!feeder.erje.net!eu.feeder.erje.net!eternal-september.org!feeder.eternal-september.org!mx05.eternal-september.org!barmar.motzarella.org!.POSTED!not-for-mail Original-Newsgroups: gnu.emacs.help Original-Lines: 48 Injection-Info: barmar.motzarella.org; posting-host="2be9e9f5dd9af768b8861af71b85fc28"; logging-data="6508"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/rGEHZG9nW2LdRsGCTMqo6" User-Agent: MT-NewsWatcher/3.5.3b3 (Intel Mac OS X) Cancel-Lock: sha1:Y0wi/EkgZu/2BQfbUrdPALnQz4Q= Original-Xref: usenet.stanford.edu gnu.emacs.help:198666 X-Mailman-Approved-At: Mon, 20 May 2013 21:58:28 -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:90931 Archived-At: In article , Sean McAfee 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 ***