all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* Error: add-to-list cannot use lexical var ...
@ 2012-08-14  5:24 Jambunathan K
  2012-08-14 12:48 ` Stefan Monnier
  0 siblings, 1 reply; 2+ messages in thread
From: Jambunathan K @ 2012-08-14  5:24 UTC (permalink / raw)
  To: emacs-devel


I happened to make some modifications to vc-dir.el recently and tried
using `add-to-list' as part of these modifications.

Add the below snippet somewhere in vc-dir.el and byte-compile it.
    (let (l)
      (add-to-list 'l "testing"))

You will see an error like.
    vc-dir.el:54:1:Warning: Unused lexical variable `l'
    vc-dir.el:54:7:Error: add-to-list cannot use lexical var `l'

I should say the results are not along the expected lines.  Can someone
explain it to me?
-- 



^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: Error: add-to-list cannot use lexical var ...
  2012-08-14  5:24 Error: add-to-list cannot use lexical var Jambunathan K
@ 2012-08-14 12:48 ` Stefan Monnier
  0 siblings, 0 replies; 2+ messages in thread
From: Stefan Monnier @ 2012-08-14 12:48 UTC (permalink / raw)
  To: Jambunathan K; +Cc: emacs-devel

>     (let (l)
>       (add-to-list 'l "testing"))

Functions like add-to-list and `set' rely on an equivalence between
symbol objects and variables which is incompatible with lexical scoping.

> You will see an error like.
>     vc-dir.el:54:1:Warning: Unused lexical variable `l'
>     vc-dir.el:54:7:Error: add-to-list cannot use lexical var `l'

> I should say the results are not along the expected lines.  Can someone
> explain it to me?

When interpreted, add-to-list could be made to work by looking up the
lexical environment, searching for the binding of `l'.
But when byte-compiled there's no such option because the byte-code
doesn't even remember that the local var was named `l'.

You can use CL's pushnew (with :test 'equal) instead.


        Stefan



^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2012-08-14 12:48 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2012-08-14  5:24 Error: add-to-list cannot use lexical var Jambunathan K
2012-08-14 12:48 ` Stefan Monnier

Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.