unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
From: Stefan Monnier <monnier@iro.umontreal.ca>
To: help-gnu-emacs@gnu.org
Subject: Re: emacs 24 lexical bindings question: top level lexical bindings
Date: Mon, 07 Nov 2011 09:49:27 -0500	[thread overview]
Message-ID: <jwvboso9f0k.fsf-monnier+gnu.emacs.help@gnu.org> (raw)
In-Reply-To: 87obwomz1q.fsf@catphive.net

> I have some code like this:
> ;;; -*- lexical-binding: t -*-
> (let ((my-var))
>   (defun f1 () ...)
>   (defun f2 () ...))

> when compiling generates this warning:
> c5-util.el:59:1:Warning: Function c5-flymake-post-command-action will ignore
>     its context (my-var)

> and a bunch of warnings for unused lexical variables... which I actually
> do use.

> So my question is... what is the compiler trying to tell me?

It is tell you that the function c5-flymake-post-command-action
won't actually use the `my-var' like your code obviously intended.

> Are top level lexical bindings not allowed?

They are, but not with `defun'.  I.e. it will work if you use

   ;;; -*- lexical-binding: t -*-
   (let ((my-var))
     (defalias 'f1 (lambda () ...)
     (defalias 'f2 (lambda () ...))

> At first I thought that the compiler was transforming my setq's of my-var in f1
> and f2 into assignments to global variables... but C-h v my-var doesn't
> show anything.  Also, my program is working correctly, so I'm leaning
> more towards the theory that these warnings themselves are bugs, and
> Emacs handles top level lexical scoping correctly.

I'm surprised it works correctly.  In your above code, the let will
create a "lexical" binding for `my-var', but uses of `my-var' within the
`defun's will not refer to that lexical binding but will look for
a `my-var' dynamic binding instead.

This is not a feature of the new code, just a limitation due to details
of how docstrings are saved (yup, it's that silly) which makes the
defalias form less memory efficient.  Hopefully this problem can be
resolved at some point.


        Stefan



           reply	other threads:[~2011-11-07 14:49 UTC|newest]

Thread overview: expand[flat|nested]  mbox.gz  Atom feed
 [parent not found: <87obwomz1q.fsf@catphive.net>]

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

  List information: https://www.gnu.org/software/emacs/

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

  git send-email \
    --in-reply-to=jwvboso9f0k.fsf-monnier+gnu.emacs.help@gnu.org \
    --to=monnier@iro.umontreal.ca \
    --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.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).