all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Deokhwan Kim <deokhwankim@gmail.com>
To: help-gnu-emacs@gnu.org
Subject: Surrounding Lexical Variable Reference in the Body of defun
Date: Mon, 20 Aug 2012 23:07:00 -0700 (PDT)	[thread overview]
Message-ID: <8b43586d-2ad4-4cf6-ae15-446e909bf496@googlegroups.com> (raw)

Hi there,

I'm having trouble understanding lexical binding in Emacs 24. I came across the following sentence in the Emacs Lisp manual <http://www.gnu.org/software/emacs/manual/html_node/elisp/Lexical-Binding.html>:

> 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


             reply	other threads:[~2012-08-21  6:07 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-08-21  6:07 Deokhwan Kim [this message]
2012-08-22  6:42 ` Surrounding Lexical Variable Reference in the Body of defun Andreas Röhler
2012-08-22 15:11 ` Stefan Monnier
2012-08-23  2:56   ` justinhj
2012-08-23 12:52   ` Deokhwan Kim

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=8b43586d-2ad4-4cf6-ae15-446e909bf496@googlegroups.com \
    --to=deokhwankim@gmail.com \
    --cc=help-gnu-emacs@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.