From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Stefan Monnier Newsgroups: gmane.emacs.devel Subject: Re: Trouble with lexical-binding. Date: Mon, 13 Apr 2015 18:28:47 -0400 Message-ID: References: <20150413220317.GC6324@acm.fritz.box> NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: text/plain X-Trace: ger.gmane.org 1428964150 8860 80.91.229.3 (13 Apr 2015 22:29:10 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Mon, 13 Apr 2015 22:29:10 +0000 (UTC) Cc: emacs-devel@gnu.org To: Alan Mackenzie Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Tue Apr 14 00:29:02 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 1Yhmqh-0001RU-I3 for ged-emacs-devel@m.gmane.org; Tue, 14 Apr 2015 00:28:59 +0200 Original-Received: from localhost ([::1]:53597 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Yhmqg-0001M9-TQ for ged-emacs-devel@m.gmane.org; Mon, 13 Apr 2015 18:28:58 -0400 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:48239) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Yhmqd-0001M2-MK for emacs-devel@gnu.org; Mon, 13 Apr 2015 18:28:56 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1YhmqZ-0007Hg-La for emacs-devel@gnu.org; Mon, 13 Apr 2015 18:28:55 -0400 Original-Received: from pruche.dit.umontreal.ca ([132.204.246.22]:48187) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1YhmqZ-0007HV-Gb for emacs-devel@gnu.org; Mon, 13 Apr 2015 18:28:51 -0400 Original-Received: from pastel.home (lechon.iro.umontreal.ca [132.204.27.242]) by pruche.dit.umontreal.ca (8.14.1/8.14.1) with ESMTP id t3DMSlTn027867; Mon, 13 Apr 2015 18:28:48 -0400 Original-Received: by pastel.home (Postfix, from userid 20848) id C361F1078; Mon, 13 Apr 2015 18:28:47 -0400 (EDT) In-Reply-To: <20150413220317.GC6324@acm.fritz.box> (Alan Mackenzie's message of "Mon, 13 Apr 2015 22:03:17 +0000") User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/25.0.50 (gnu/linux) X-NAI-Spam-Flag: NO X-NAI-Spam-Threshold: 5 X-NAI-Spam-Score: 0 X-NAI-Spam-Rules: 1 Rules triggered RV5275=0 X-NAI-Spam-Version: 2.3.0.9393 : core <5275> : inlines <2702> : streams <1421999> : uri <1905954> X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 132.204.246.22 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:185390 Archived-At: > 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) (eval-when-compile (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