From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Deokhwan Kim Newsgroups: gmane.emacs.help Subject: Surrounding Lexical Variable Reference in the Body of defun Date: Mon, 20 Aug 2012 23:07:00 -0700 (PDT) Organization: http://groups.google.com Message-ID: <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 Content-Transfer-Encoding: quoted-printable X-Trace: ger.gmane.org 1345529413 32542 80.91.229.3 (21 Aug 2012 06:10:13 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Tue, 21 Aug 2012 06:10:13 +0000 (UTC) To: help-gnu-emacs@gnu.org Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Tue Aug 21 08:10:13 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 1T3hfJ-0004eV-NV for geh-help-gnu-emacs@m.gmane.org; Tue, 21 Aug 2012 08:10:13 +0200 Original-Received: from localhost ([::1]:60759 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1T3hfI-0007nz-82 for geh-help-gnu-emacs@m.gmane.org; Tue, 21 Aug 2012 02:10:12 -0400 Original-Path: usenet.stanford.edu!postnews.google.com!glegroupsg2000goo.googlegroups.com!not-for-mail Original-Newsgroups: gnu.emacs.help Original-Lines: 58 Original-NNTP-Posting-Host: 18.95.7.156 Original-X-Trace: posting.google.com 1345529341 21585 127.0.0.1 (21 Aug 2012 06:09:01 GMT) Original-X-Complaints-To: groups-abuse@google.com Original-NNTP-Posting-Date: Tue, 21 Aug 2012 06:09:01 +0000 (UTC) Complaints-To: groups-abuse@google.com Injection-Info: glegroupsg2000goo.googlegroups.com; posting-host=18.95.7.156; posting-account=FaNF-woAAABS3ImH9CZqJhP92J5_94lR User-Agent: G2/1.0 Original-Xref: usenet.stanford.edu gnu.emacs.help:194075 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:86441 Archived-At: 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 l= exical 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 mis= understand what the sentence really meant. So I decided to make some experi= ments 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 war= ning 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 c= laims: $ 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 des= ign decision of Emacs? Or is it temporary and removed in a (near) future re= lease? 2. Why does the original source code behave differently from its compiled= code? Best regards, Deokhwan Kim