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: Is add-to-list supposed to work when lexical-binding is t?
Date: Mon, 3 Jun 2013 17:42:09 -0700 (PDT)	[thread overview]
Message-ID: <1370306529.4188.YahooMailClassic@web141101.mail.bf1.yahoo.com> (raw)

Section 11.9.3 (Lexical Binding) in the manual says "functions like `symbol-value', `boundp', and `set' only retrieve or modify a variable's dynamic binding". Why? It causes chaos:
(setq lexical-binding nil)
(let ((x '(a))) (add-to-list 'x 'b) x) -> (b a)
(setq lexical-binding t)
(let ((x '(a))) (add-to-list 'x 'b) x) -> Lisp error: (void-variable x)

(setq lexical-binding nil)
(defun foo (var) (set var 'b))
(let ((x 'a)) (foo 'x) x) -> b
(setq lexical-binding t)
(let ((x 'a)) (foo 'x) x) -> a (also does global (set 'x 'b))

Neither foo nor the let form references any free variables. Ironic that (let ((x 'a)) (foo 'x)) without lexical binding doesn't (permanently) set global x, but with it does. Lexical binding is supposed to reduce such accidents, not cause them.

I get the same result in SBCL, so it isn't just an elisp problem. C can do it:
char x='z'; 
void foo (char* var) {*var='b';}
int main () { {
      char x='a'; foo(&x);
      printf ("Local x: %c, ", x);}
    printf ("Global x: %c\n", x);} -> "Local x: b, Global x: z"

Considering that (intern "x") returns global x, and locals are not eq to globals, it appears (quote x) also returns global x, not lexical x:
(setq lexical-binding t)
(let ((x 'a)) (eq 'x (intern "x"))) -> t
which means the problem of set and symbol-value only accessing global variables is moot, since you can't even pass lexical variables in the first place. (foo 'x) can't work with lexical x, for any foo. In Lisp, it appears you can't even say "&x".

I could avoid the problem this way:
(require 'cl)
(defmacro add-to-list (list-var element &optional append compare-fn)
  "Like function add-to-list in subr.el, but works even when `lexical-binding' is t."
  (let ((list (if (eq (car-safe list-var) 'quote)
		  (cadr list-var)
		`(symbol-value ,list-var))))
    `(if (member* ,element ,list :test (or ,compare-fn 'equal))
	 ,list
       (setf ,list
	     (if ,append
		 (append ,list (list ,element))
	       (cons ,element ,list))))))
which is gross. Or just take a list directly, instead of a list-var, but that's incompatible. Either way, converting every argument-mutating function into a macro obviously isn't the right thing to do. What is the right thing?

Maybe this:
(require 'cl)
(setq lexical-binding t)
(defmacro wrap-lexical (x)
  `(list (lambda () ,x)
	 (lambda (val) (setq ,x val))))
(defun get-passed-lexical (x)
  (funcall (car x)))
(defun set-passed-lexical (x val)
 (funcall (cadr x) val))
(defun add-to-list-lexable (wrapped-list element &optional append compare-fn)
  (let ((list (get-passed-lexical wrapped-list)))
    (if (member* element list :test (or compare-fn 'equal))
	 list
       (set-passed-lexical wrapped-list
	     (if append
		 (append list (list element))
	       (cons element list))))))
(let ((x '(a))) (add-to-list-lexable (wrap-lexical x) 'b) x) -> (b a)
but that's both gross and incompatible.




             reply	other threads:[~2013-06-04  0:42 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-06-04  0:42 Kelly Dean [this message]
2013-06-04  1:49 ` Is add-to-list supposed to work when lexical-binding is t? Stefan Monnier
2013-06-04 15:24   ` PJ Weisberg
2013-06-05  2:41     ` Stefan Monnier
  -- strict thread matches above, loose matches on Subject: below --
2013-06-05 23:12 Kelly Dean
2013-06-06  0:42 ` Stefan Monnier
2013-06-10  1:43 Kelly Dean
2013-06-10  7:56 ` Stefan Monnier
     [not found] <mailman.1311.1370828650.22516.help-gnu-emacs@gnu.org>
2013-06-10  5:12 ` Barry Margolin
2013-06-12  0:59 Kelly Dean
     [not found] <mailman.1450.1370998793.22516.help-gnu-emacs@gnu.org>
2013-06-13 14:57 ` Stefan Monnier

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=1370306529.4188.YahooMailClassic@web141101.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).