From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: =?ISO-8859-1?Q?Andreas_R=F6hler?= Newsgroups: gmane.emacs.help Subject: Re: Surrounding Lexical Variable Reference in the Body of defun Date: Wed, 22 Aug 2012 08:42:24 +0200 Message-ID: <50347F50.4000506@easy-emacs.de> References: <8b43586d-2ad4-4cf6-ae15-446e909bf496@googlegroups.com> NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Trace: ger.gmane.org 1345617770 3248 80.91.229.3 (22 Aug 2012 06:42:50 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Wed, 22 Aug 2012 06:42:50 +0000 (UTC) To: help-gnu-emacs@gnu.org Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Wed Aug 22 08:42:47 2012 Return-path: Envelope-to: geh-help-gnu-emacs@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 1T44eM-0002ST-Vr for geh-help-gnu-emacs@m.gmane.org; Wed, 22 Aug 2012 08:42:47 +0200 Original-Received: from localhost ([::1]:36834 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1T44eL-0002iZ-FI for geh-help-gnu-emacs@m.gmane.org; Wed, 22 Aug 2012 02:42:45 -0400 Original-Received: from eggs.gnu.org ([208.118.235.92]:33977) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1T44eA-0002iP-Ex for help-gnu-emacs@gnu.org; Wed, 22 Aug 2012 02:42:40 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1T44e6-0003RD-Hc for help-gnu-emacs@gnu.org; Wed, 22 Aug 2012 02:42:34 -0400 Original-Received: from moutng.kundenserver.de ([212.227.126.171]:64403) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1T44e6-0003R3-7u for help-gnu-emacs@gnu.org; Wed, 22 Aug 2012 02:42:30 -0400 Original-Received: from [192.168.178.27] (brln-4dbc57df.pool.mediaWays.net [77.188.87.223]) by mrelayeu.kundenserver.de (node=mreu4) with ESMTP (Nemesis) id 0M6c88-1TpQ062aHs-00wVxB; Wed, 22 Aug 2012 08:42:28 +0200 User-Agent: Mozilla/5.0 (X11; Linux i686; rv:14.0) Gecko/20120713 Thunderbird/14.0 In-Reply-To: <8b43586d-2ad4-4cf6-ae15-446e909bf496@googlegroups.com> X-Provags-ID: V02:K0:1X28wCZN4uMD6fjYJGM0ZYIzQpM/Wxf+5nNSxjsIlnN Miwbx4hWe5ebfQ0SblqMXKqHo8uWM8B0yDSoI1aZeim32Eyp63 AjgPMU92M94hCgZ4EvxBQADPC82vB7POO73/KC6F72If0ULjaI foeB1gsLr99LDRyoKsu0at3rg3IYPzlSS1zIrmcJjBtOQu3DP3 K490kQLvSvIjfyp/uGbhrZzcuUTKudMY594FEwJAB1Qhd4t2hI HojZ8owclKqKVPvLqpIJWB1uq+j1ICQj8hh0zFlTpjl2EHEPeg /78Bwi29O7x24GaSLqMH/jNbKs/dakX9RWG0VVDUmdtZN0T5wa OcS4DjwJj6rLp7njLF1Jpuw41Rz72ibqKEzp+Pj/A X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 212.227.126.171 X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Original-Sender: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.help:86461 Archived-At: Am 21.08.2012 08:07, schrieb Deokhwan Kim: > Hi there, > > I'm having trouble understanding lexical binding in Emacs 24. I came across the following sentence in the Emacs Lisp manual : > >> the code in the body of a defun or defmacro cannot refer to surrounding lexical variables. > > It was a great shock to me because it sounded quite awkward and Common Lisp does not have such restriction AFAIK. Rather, I suspected that I might misunderstand what the sentence really meant. So I decided to make some experiments with the following code stored in foo.el: > > ;;; -*- lexical-binding: t -*- > (let ((x 0)) > (defun counter () > (setq x (1+ x)))) > > (message "%d" (counter)) > (message "%d" (counter)) > > Surprisingly, when I ran it in the form of source code, it worked: > > $ emacs -Q -batch -l foo.el > 1 > 2 > > On the other hand, when I tried to byte-compile it, I got the following warning messages: > > $ emacs -Q -batch -f batch-byte-compile foo.el > In toplevel form: > foo.el:2:1:Warning: Function counter will ignore its context (x) > foo.el:2:1:Warning: Unused lexical variable `x' > foo.el:4:11:Warning: reference to free variable `x' > foo.el:4:17:Warning: assignment to free variable `x' > > In end of data: > foo.el:8:1:Warning: the function `counter' is not known to be defined. > Wrote foo.elc > > When I ran the resulting byte-compiled code, I got an error as the manual claims: > > $ emacs -Q -batch -l foo.elc > Symbol's value as variable is void: x > > Now I'm so confused. Here are my two questions: > > 1. Why does this restriction exists? Is it inevitable because of some design decision of Emacs? Or is it temporary and removed in a (near) future release? > 2. Why does the original source code behave differently from its compiled code? > > Best regards, > Deokhwan Kim > as this question isn't raised first time, maybe Emacs developers consider to revert that change?