unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
* Re: emacs 24 lexical bindings question: top level lexical bindings
       [not found] <87obwomz1q.fsf@catphive.net>
@ 2011-11-07 14:49 ` Stefan Monnier
  0 siblings, 0 replies; only message in thread
From: Stefan Monnier @ 2011-11-07 14:49 UTC (permalink / raw)
  To: help-gnu-emacs

> 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



^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2011-11-07 14:49 UTC | newest]

Thread overview: (only message) (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
     [not found] <87obwomz1q.fsf@catphive.net>
2011-11-07 14:49 ` emacs 24 lexical bindings question: top level lexical bindings Stefan Monnier

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).