unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
From: Kelly Dean <kellydeanch@yahoo.com>
To: help-gnu-emacs@gnu.org
Subject: Unreferenced symbols in closures, and a problem with closures in minor modes
Date: Tue, 28 May 2013 15:35:58 -0700 (PDT)	[thread overview]
Message-ID: <1369780558.54419.YahooMailClassic@web141103.mail.bf1.yahoo.com> (raw)

(setq lexical-binding t)

As expected, (let ((x 0)) (lambda () x)) -> (closure ((x . 0) t) nil x)
But, (let ((x 0)) (lambda () 0)) -> (closure ((x . 0) t) nil 0)
And, (lambda (x) x) -> (closure (t) (x) x)

Why do closures' lexical environments include unreferenced symbols? And why produce a closure with an empty environment, instead of just a lambda form?

And I have a problem with unwanted closures in minor modes:
(defvar foo-hook nil)
(define-minor-mode bar "Bar" nil " Bar" nil
  (if bar (add-hook 'foo-hook (lambda () (bar 0)) nil t)
    (remove-hook 'foo-hook (lambda () (bar 0)) t)))

M-x bar RET C-h v foo-hook RET, and I get:
foo-hook's value is ((closure
  ((last-message)
   (arg . toggle)
   t)
  nil
  (bar 0))
 t)

Which won't be removed when foo-hook runs, because arg will be 0, not 'toggle. I tried to thwart the closure like this:
(defvar foo2-hook nil)
(define-minor-mode bar2 "Bar2" nil " Bar2" nil
  (if bar2 (add-hook 'foo2-hook (let ((arg 0)) (lambda () (bar2 0))) nil t)
    (remove-hook 'foo2-hook (let ((arg 0)) (lambda () (bar2 0))) t)))

M-x bar2 RET C-h v foo2-hook RET, and I get:
foo2-hook's value is ((closure
  ((arg . 0)
   (last-message)
   (arg . toggle)
   t)
  nil
  (bar2 0))
 t)

Argh! Surely this is a misfeature?

Also I don't know why last-message is there, but at least it doesn't interfere.

On a previous experiment that I can't reproduce, involving several additions and removals, I got:
Value: ((closure
  ((last-message)
   (arg . 1)
   i t)
  nil
  (bar 0))
 (closure
  ((last-message)
   (arg . 1)
   i t)
  nil
  (bar 0))
 t)

I have no idea what the symbol i means or where it came from. If it were setting i to nil in the lexical environment, it would be (i), not i.

Also, the two closures are identical, yet add-hook isn't supposed to add duplicates.

Help?




             reply	other threads:[~2013-05-28 22:35 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-05-28 22:35 Kelly Dean [this message]
2013-05-29  1:28 ` Unreferenced symbols in closures, and a problem with closures in minor modes Stefan Monnier
  -- strict thread matches above, loose matches on Subject: below --
2013-05-29  4:44 Kelly Dean

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=1369780558.54419.YahooMailClassic@web141103.mail.bf1.yahoo.com \
    --to=kellydeanch@yahoo.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).