From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Alan Mackenzie Newsgroups: gmane.emacs.devel Subject: Re: Trouble with lexical-binding. Date: Tue, 14 Apr 2015 14:03:13 +0000 Message-ID: <20150414140313.GA4612@acm.fritz.box> References: <20150413220317.GC6324@acm.fritz.box> NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Trace: ger.gmane.org 1429020234 29262 80.91.229.3 (14 Apr 2015 14:03:54 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Tue, 14 Apr 2015 14:03:54 +0000 (UTC) Cc: emacs-devel@gnu.org To: Philipp Stephani Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Tue Apr 14 16:03:44 2015 Return-path: Envelope-to: ged-emacs-devel@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by plane.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1Yi1RH-0000l2-HR for ged-emacs-devel@m.gmane.org; Tue, 14 Apr 2015 16:03:43 +0200 Original-Received: from localhost ([::1]:56071 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Yi1RG-00031c-Mo for ged-emacs-devel@m.gmane.org; Tue, 14 Apr 2015 10:03:42 -0400 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:57021) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Yi1Qx-00031H-9Z for emacs-devel@gnu.org; Tue, 14 Apr 2015 10:03:28 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1Yi1Qq-00084Q-BR for emacs-devel@gnu.org; Tue, 14 Apr 2015 10:03:23 -0400 Original-Received: from colin.muc.de ([193.149.48.1]:12544 helo=mail.muc.de) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Yi1Qq-00084H-2F for emacs-devel@gnu.org; Tue, 14 Apr 2015 10:03:16 -0400 Original-Received: (qmail 57709 invoked by uid 3782); 14 Apr 2015 14:03:14 -0000 Original-Received: from acm.muc.de (p579E829F.dip0.t-ipconnect.de [87.158.130.159]) by colin.muc.de (tmda-ofmipd) with ESMTP; Tue, 14 Apr 2015 16:03:13 +0200 Original-Received: (qmail 4678 invoked by uid 1000); 14 Apr 2015 14:03:13 -0000 Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.5.23 (2014-03-12) X-Delivery-Agent: TMDA/1.1.12 (Macallan) X-Primary-Address: acm@muc.de X-detected-operating-system: by eggs.gnu.org: FreeBSD 9.x X-Received-From: 193.149.48.1 X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: "Emacs development discussions." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Original-Sender: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.devel:185403 Archived-At: 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).