From: pjb@informatimago.com (Pascal J. Bourguignon)
To: help-gnu-emacs@gnu.org
Subject: Re: How do I remove "reference to free variable" warnings on buffer-local variables?
Date: Mon, 09 Nov 2009 18:40:42 +0100 [thread overview]
Message-ID: <87zl6v61gl.fsf@galatea.local> (raw)
In-Reply-To: aa6ede77-acba-4443-b2de-a249b19c4e6c@k4g2000yqb.googlegroups.com
rocky <rocky@gnu.org> writes:
> I have code that uses buffer local variables. I don't want to declare
> this variable global. So how can I remove messages of the form
> "reference to free variable `...' " when I byte compile a file?
A free variable is a variable that is used inside a function that is
not defined locally. It must be considered a global variable.
As indicated in the other answers, if no such global variable is
declared, then you get this warning.
The point is that most often, you don't want a global variable
(otherwise you would have used defvar to define it), but you want a
local variable, and you used setq or setf instead of let.
Use let (or let*) to define local variables. Instead of writing:
(defun equa2 (a b c)
(setq delta (- (* b b) (* 4 a c)))
(cond ((< delta 0) '())
((= delta 0) (list (/ (- b) 2 a)))
(t (list (/ (+ (- b) (sqrt delta)) 2 a)
(/ (- (- b) (sqrt delta)) 2 a)))))
write:
(defun equa2 (a b c)
(let ((delta (- (* b b) (* 4 a c))))
(cond ((< delta 0) '())
((= delta 0) (list (/ (- b) 2 a)))
(t (list (/ (+ (- b) (sqrt delta)) 2 a)
(/ (- (- b) (sqrt delta)) 2 a))))))
In general, try to avoid setq or setf, and rather use the functional
programming style.
--
__Pascal Bourguignon__
next prev parent reply other threads:[~2009-11-09 17:40 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2009-11-09 15:10 How do I remove "reference to free variable" warnings on buffer-local variables? rocky
2009-11-09 16:45 ` Tassilo Horn
2009-11-09 17:40 ` How do I remove "reference to free variable" warnings onbuffer-local variables? Drew Adams
2009-11-09 18:58 ` Tassilo Horn
[not found] ` <mailman.10351.1257785158.2239.help-gnu-emacs@gnu.org>
2009-11-09 17:28 ` How do I remove "reference to free variable" warnings on buffer-local variables? rocky
2009-11-09 17:40 ` Pascal J. Bourguignon [this message]
2009-11-11 5:09 ` Stefan Monnier
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=87zl6v61gl.fsf@galatea.local \
--to=pjb@informatimago.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.