[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Returning variable "references" under lexical binding
From: |
Barry Margolin |
Subject: |
Re: Returning variable "references" under lexical binding |
Date: |
Tue, 21 May 2013 14:06:23 -0400 |
User-agent: |
MT-NewsWatcher/3.5.3b3 (Intel Mac OS X) |
In article <87bo84b85k.fsf@gmail.com>, Sean McAfee <eefacm@gmail.com>
wrote:
> Barry Margolin <barmar@alum.mit.edu> 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 ***
- 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, 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 <=
- Re: Returning variable "references" under lexical binding, Sean McAfee, 2013/05/21
- Re: Returning variable "references" under lexical binding, Stefan Monnier, 2013/05/22