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: Tue, 21 May 2013 14:06:23 -0400 Organization: A noiseless patient Spider Message-ID: References: <87fvxgc2mz.fsf@gmail.com> <87bo84b85k.fsf@gmail.com> NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 X-Trace: ger.gmane.org 1369160669 26903 80.91.229.3 (21 May 2013 18:24:29 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Tue, 21 May 2013 18:24:29 +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 20:24:29 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 1UerEb-0007wU-H7 for geh-help-gnu-emacs@m.gmane.org; Tue, 21 May 2013 20:24:29 +0200 Original-Received: from localhost ([::1]:59973 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1UerEa-0000A1-GP for geh-help-gnu-emacs@m.gmane.org; Tue, 21 May 2013 14:24:28 -0400 X-Received: by 10.180.206.107 with SMTP id ln11mr3674199wic.7.1369159584170; Tue, 21 May 2013 11:06:24 -0700 (PDT) X-FeedAbuse: http://nntpfeed.proxad.net/abuse.pl feeded by 88.191.116.97 Original-Path: usenet.stanford.edu!h2no20525022wiw.1!news-out.google.com!hv6ni7240wib.1!nntp.google.com!feeder1-2.proxad.net!proxad.net!feeder2-2.proxad.net!nntpfeed.proxad.net!dedibox.gegeweb.org!gegeweb.eu!gegeweb.org!de-l.enfer-du-nord.net!feeder2.enfer-du-nord.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: 44 Injection-Info: barmar.motzarella.org; posting-host="2be9e9f5dd9af768b8861af71b85fc28"; logging-data="3328"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+1Gm4bvKj8JZIaaaxQhwzn" User-Agent: MT-NewsWatcher/3.5.3b3 (Intel Mac OS X) Cancel-Lock: sha1:ps1CvaRFjDnr9CO7rbOCaxvZGAw= Original-Xref: usenet.stanford.edu gnu.emacs.help:198694 X-Mailman-Approved-At: Tue, 21 May 2013 14:24:17 -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:90959 Archived-At: In article <87bo84b85k.fsf@gmail.com>, Sean McAfee wrote: > Barry Margolin writes: > > But it's not the same lexical variable in your example. Each time you > > call start-my-timer you're creating a new closure over that variable. > > The caller has to save that closure somewhere, so that it can pass it to > > cancel-my-timer later. There's no functional difference between that and > > saving the timer itself. > > Sure there is. OK, forget about timers, let's go even simpler: > > ;; -*- lexical-binding: t; -*- > > (defun return-variable-n () > (let ((n 1)) > (in-five-seconds (lambda () (setq n 2))) > SOMETHING)) > > (setq foo (return-variable-n)) > > I want to return SOMETHING such that if I inspect it immediately, I'll > get 1, but if I save it and inspect it after five seconds have passed, > I'll get 2. SOMETHING can't be just "n", because the function returns > by value and foo would only ever contain 1. This is different, because now you have something with state that can change, and want to be able to inspect. it. > > Similarly, in my original code, I can't just return "timer", because the > calling code would only ever see the very first timer created, and would > not be able to see the new values for timer that the callbacks store > later. What "new values"? Your code didn't have any way to change the value of timer. All it did was wrap it in a closure, but that closure doesn't do anything other than return the initial value. Did you leave something out of the original example? -- Barry Margolin, barmar@alum.mit.edu Arlington, MA *** PLEASE post questions in newsgroups, not directly to me ***