[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Trouble with lexical-binding.

From: Stefan Monnier
Subject: Re: Trouble with lexical-binding.
Date: Mon, 13 Apr 2015 18:28:47 -0400
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/25.0.50 (gnu/linux)

> Obviously, this bit of the code needs dynamic binding.  So I should be
> able to bind `lexical-binding' to nil at some strategic place to achieve

No: lexical-binding is not a variable you can bind at run-time.
It happens to be implemented as an Emacs variable, but it's really
a config parameter to the compiler, so once the code is loaded all of
that code is "irremediably" lexically scoped.

But the fix is easy: declare that `ptr' should be a dynamically scoped
variable with a simple:

    (defvar ptr)

Of course, doing so is strongly discouraged, since it could break other
code which uses `ptr' and expects lexical binding for it.
So better add a package prefix to it to avoid those conflicts:

   ;;; lexical-bug.el --- lexical bug                   -*- lexical-binding: t; 
   (defvar mypkg--ptr)
     (defmacro test-ptr (x)
       `(let* ((mypkg--ptr (copy-tree ,x))
               (form '(setcar mypkg--ptr 'a))
               (result (eval form)))
          (message "result is %s, ptr is %s" result mypkg--ptr))))
   (test-ptr '(x y z))

-- Stefan

reply via email to

[Prev in Thread] Current Thread [Next in Thread]