emacs-devel
[Top][All Lists]
Advanced

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

Re: Trouble with lexical-binding.


From: Alan Mackenzie
Subject: Re: Trouble with lexical-binding.
Date: Tue, 14 Apr 2015 14:03:13 +0000
User-agent: Mutt/1.5.23 (2014-03-12)

Hello, Philipp

On Mon, Apr 13, 2015 at 10:19:14PM +0000, Philipp Stephani wrote:

> IIUC only the local variable is relevant. At least that's the case for
> loading, and it would make sense for byte compilation as well. Byte
> compilation transforms the lexical binding into a stack access and gets rid
> of the 'ptr' symbol entirely.


> > What do I have to do to get `ptr' in this context a special variable
> > (whilst still having it lexically bound in other code)?


> The correct way is to avoid eval and use a closure instead:

>  (let* ((ptr (copy-tree ,x))
>             (form (lambda () (setcar ptr 'a)))
>             (result (funcall form)))
>        (message "result is %s, ptr is %s" result ptr))

Thanks, this (almost) works.  In the end, in my actual code, I needed to
put a backquote on (lambda ...), so as to be able to evaluate something
inside it with a comma.  There's always a way.

> Not only will this work with lexical binding, the byte compiler is able to
> provide better error messages (e.g. if you try to call an undefined
> function in the form) and enable additional optimizations.

Yes.  And it even works without lexical binding, which is a bonus.

-- 
Alan Mackenzie (Nuremberg, Germany).



reply via email to

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