unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
From: Sean McAfee <eefacm@gmail.com>
To: help-gnu-emacs@gnu.org
Subject: Re: Returning variable "references" under lexical binding
Date: Tue, 21 May 2013 09:38:15 -0700	[thread overview]
Message-ID: <87bo84b85k.fsf@gmail.com> (raw)
In-Reply-To: barmar-FDE050.10235921052013@news.eternal-september.org

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.

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.

Without lexical variables, I could do this:

(defun return-variable-n ()
  (let ((n (gensym)))
    (set n 1)
    (in-five-seconds (lambda () (set n 2)))
    n))

...and then get the current value of n using symbol-value.

Using lexical variables, SOMETHING can be (lambda () n), which can be
called to get the current value of n at all times.

My question was whether other methods exist, and which is best, for
reasonable definitions of "best."


  reply	other threads:[~2013-05-21 16:38 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-05-20 20:35 Returning variable "references" under lexical binding Sean McAfee
2013-05-21  1:18 ` Barry Margolin
2013-05-21  3:18 ` Stefan Monnier
     [not found] ` <mailman.112.1369106352.22516.help-gnu-emacs@gnu.org>
2013-05-21  5:39   ` Sean McAfee
2013-05-21 12:54     ` Stefan Monnier
2013-05-21 14:23     ` Barry Margolin
2013-05-21 16:38       ` Sean McAfee [this message]
2013-05-21 18:06         ` Barry Margolin
2013-05-21 22:43           ` Sean McAfee
2013-05-23  1:01             ` Stefan Monnier
  -- strict thread matches above, loose matches on Subject: below --
2013-05-21 14:41 Barry OReilly

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87bo84b85k.fsf@gmail.com \
    --to=eefacm@gmail.com \
    --cc=help-gnu-emacs@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).