From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: pjb@informatimago.com (Pascal J. Bourguignon) Newsgroups: gmane.emacs.help Subject: Re: How do I remove "reference to free variable" warnings on buffer-local variables? Date: Mon, 09 Nov 2009 18:40:42 +0100 Organization: Informatimago Message-ID: <87zl6v61gl.fsf@galatea.local> References: NNTP-Posting-Host: lo.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Trace: ger.gmane.org 1257792067 31406 80.91.229.12 (9 Nov 2009 18:41:07 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Mon, 9 Nov 2009 18:41:07 +0000 (UTC) To: help-gnu-emacs@gnu.org Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Mon Nov 09 19:41:00 2009 Return-path: Envelope-to: geh-help-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([199.232.76.165]) by lo.gmane.org with esmtp (Exim 4.50) id 1N7ZAU-0005Cv-5N for geh-help-gnu-emacs@m.gmane.org; Mon, 09 Nov 2009 19:40:46 +0100 Original-Received: from localhost ([127.0.0.1]:41954 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1N7ZAT-0001qG-KW for geh-help-gnu-emacs@m.gmane.org; Mon, 09 Nov 2009 13:40:45 -0500 Original-Path: news.stanford.edu!usenet.stanford.edu!news.tele.dk!news.tele.dk!small.news.tele.dk!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail Original-Newsgroups: gnu.emacs.help Original-Lines: 41 Original-X-Trace: individual.net aZzgIS65UA6kiFt+gdtKLAKWgTMBzANG4+/WlMiyZCNMIQpNJL Cancel-Lock: sha1:Y2FjNGJjOThkOGNhNGU0ZTZhYzYyOTAxNjM4OTgxYTBlMDY1NzE3MA== sha1:OKA6OZNi6SON1nBZfM0xHUc5wNI= Face: iVBORw0KGgoAAAANSUhEUgAAADAAAAAwAQMAAABtzGvEAAAABlBMVEUAAAD///+l2Z/dAAAA oElEQVR4nK3OsRHCMAwF0O8YQufUNIQRGIAja9CxSA55AxZgFO4coMgYrEDDQZWPIlNAjwq9 033pbOBPtbXuB6PKNBn5gZkhGa86Z4x2wE67O+06WxGD/HCOGR0deY3f9Ijwwt7rNGNf6Oac l/GuZTF1wFGKiYYHKSFAkjIo1b6sCYS1sVmFhhhahKQssRjRT90ITWUk6vvK3RsPGs+M1RuR mV+hO/VvFAAAAABJRU5ErkJggg== X-Accept-Language: fr, es, en X-Disabled: X-No-Archive: no User-Agent: Gnus/5.1008 (Gnus v5.10.8) Emacs/22.3 (darwin) Original-Xref: news.stanford.edu gnu.emacs.help:174558 X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Original-Sender: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Errors-To: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.help:69633 Archived-At: rocky 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__