From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: "Pascal J. Bourguignon" Newsgroups: gmane.emacs.help Subject: Re: Declaring a local dynamic variable? Date: Sun, 22 Sep 2013 19:11:36 +0200 Organization: Informatimago Message-ID: <878uyo4w8n.fsf@informatimago.com> References: NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Trace: ger.gmane.org 1379870118 31607 80.91.229.3 (22 Sep 2013 17:15:18 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Sun, 22 Sep 2013 17:15:18 +0000 (UTC) To: help-gnu-emacs@gnu.org Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Sun Sep 22 19:15:20 2013 Return-path: Envelope-to: geh-help-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by plane.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1VNnFe-0001f9-DZ for geh-help-gnu-emacs@m.gmane.org; Sun, 22 Sep 2013 19:15:18 +0200 Original-Received: from localhost ([::1]:36079 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1VNnFd-0003c1-VC for geh-help-gnu-emacs@m.gmane.org; Sun, 22 Sep 2013 13:15:17 -0400 Original-Path: usenet.stanford.edu!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail Original-Newsgroups: gnu.emacs.help Original-Lines: 197 Original-X-Trace: individual.net Iw+LFxhDLGQxhfubvcqpVQQ9O9DK7aq3bHdms0RSa1tlHhUkvG Cancel-Lock: sha1:MDdjMjUyZGVmODJjMmJkMGQwMTAwNTFhYTYyMTI5M2UwZDIyNDkwNA== sha1:2UcADhZtXRZvsBVyODgW7zZZv6A= Face: iVBORw0KGgoAAAANSUhEUgAAADAAAAAwAQMAAABtzGvEAAAABlBMVEUAAAD///+l2Z/dAAAA oElEQVR4nK3OsRHCMAwF0O8YQufUNIQRGIAja9CxSA55AxZgFO4coMgYrEDDQZWPIlNAjwq9 033pbOBPtbXuB6PKNBn5gZkhGa86Z4x2wE67O+06WxGD/HCOGR0deY3f9Ijwwt7rNGNf6Oac l/GuZTF1wFGKiYYHKSFAkjIo1b6sCYS1sVmFhhhahKQssRjRT90ITWUk6vvK3RsPGs+M1RuR mV+hO/VvFAAAAABJRU5ErkJggg== X-Accept-Language: fr, es, en User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.2 (gnu/linux) Original-Xref: usenet.stanford.edu gnu.emacs.help:201239 X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Original-Sender: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.help:93508 Archived-At: Joost Kremers writes: > Hi, > > If I purposefully use a local dynamic variable as in: > > (defun foo () > (let* ((my-counter 0) > (var (or (bar 'one) > (bar 'two) > (bar 'three)))) > (do-something-with var) > (issue-a-message-depending-on-the-value-of my-counter))) > > (defun bar (arg) > (setq my-counter (1+ my-counter)) > (do-something-with arg) > (and-return-result)) > > the byte compiler will issue a warning about referring to a free > variable in `bar'. What is the proper way of letting the byte compiler > know that it shouldn't worry about this particular variable, without > silencing it completely? I don't want to disable all warnings, or even > just the warnings about free variables. > > Right now, I'm using a `(defvar my-counter)' inside the function > definition of `bar', but that looks a bit strange. Is there a better or > "more proper" way of doing this? CL has the (declare (special var…)) declaration for that. (defun f () (declare (special var)) (print var)) (defun g () (let ((var 42)) (declare (special var)) (f))) (g) ; prints 42. (defun h () (let ((var 33)) ; lexical variable (g))) (h) ; prints 42 To implement that in emacs lisp, you could define defun, let, let* and locally in a separate package, oops, no package in emacs. To implement that in emacs lisp, you could define defun@, let@, let*@ and locally@ as macros that will check for the special declarations and define the variables as symbol-macrolets expanding to (symbol-value 'variable), and that save the old symbol-value and restore it. Something like: (defun split-declarations (body) (loop for b on body while (and (listp (car b)) (eq 'declare (caar b))) append (cdar b) into declarations finally (return (list declarations b)))) (assert (equal (split-declarations '((declare (special a b)) (declare (integer a) (character b)) (declare (special c)) (print a))) '(((special a b) (integer a) (character b) (special c)) ((print a))))) (defun split-bindings (bindings) (list (mapcar (lambda (b) (cond ((symbolp b) b) ((or (atom b) (cddr b)) (error "Invalid binding: %S -- a binding should be a symbol or a list of two elements, a symbol and an expression." b)) (t (first b)))) bindings) (mapcar (lambda (b) (if (atom b) nil (second b))) bindings))) (assert (equal (split-bindings '(a (b 42) (c d))) '((a b c) (nil 42 d)))) (defun split-special-variables (declarations) (loop for decl in declarations if (eq 'special (car decl)) append (cdr decl) into specials else collect decl into other-decls finally (return (list specials other-decls)))) (assert (equal (split-special-variables (first (split-declarations '((declare (special a b)) (declare (integer a) (character b)) (declare (special c)) (print a))))) '((a b c) ((integer a) (character b))))) (defvar *unbound* (list 'unbound)) (defun special-bind@ (var val expression) (let ((save (gensym))) `(let ((,save (if (boundp ',var) (symbol-value ',var) *unbound*))) (unwind-protect (symbol-macrolet ((,var (symbol-value ',var))) (setf (symbol-value ',var) ,val) ,expression) (if (eq ,save *unbound*) (makunbound ',var) (setf (symbol-value ',var) ,save)))))) (defun lexical-bind@ (var val expression) `(let ((,var ,val) ,expression))) (defun bind@ (specials vars vals expression) (if (null vars) expression (bind@ specials (cdr vars) (cdr vals) (if (member (car vars) specials) (special-bind@ (car vars) (car vals) expression) (lexical-bind@ (car vars) (car vals) expression))))) (defmacro let@ (bindings &rest body) (destructuring-bind (vars vals) (split-bindings bindings) (destructuring-bind (declarations body) (split-declarations body) (destructuring-bind (special-variables declarations) (split-special-variables declarations) (let ((temps (mapcar (lambda (var) (declare (ignore var)) (gensym)) vars))) `(let ,(mapcar* (function list) temps vals) ,(bind@ special-variables (nreverse vars) (nreverse temps) `(progn ,@body)))))))) (defun specially@ (vars expression) `(symbol-macrolet ,(mapcar (lambda (var) `(,var (symbol-value ',var))) vars) ,expression)) (defmacro locally@ (&rest body) (destructuring-bind (declarations body) (split-declarations body) (message "d=%S b=%S" declarations body) (destructuring-bind (special-variables declarations) (split-special-variables declarations) (message "s=%S d=%S" special-variables declarations) (specially@ special-variables (if declarations `(locally (declare ,@declarations) ,@body) `(progn ,@body)))))) (defun f () (locally@ (declare (special x)) (print x))) (defun g () (let@ ((x 42)) (declare (special x)) (f))) (g) prints: 42 --> 42 (defun h () (let@ ((var 33)) ; lexical variable (g))) (h) prints:42 --> nil ; where does this come from? Is there a bug in emacs-version "24.2.1"? For defun@, one would have to take care of special declarations for parameters, and splitting docstrings from declarations and body. -- __Pascal Bourguignon__ http://www.informatimago.com/