From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Newsgroups: gmane.emacs.devel Subject: Re: Using the content of a dynamic variable in a macro Date: Sat, 25 Feb 2023 10:40:18 +0100 Message-ID: References: <87o7pi9m9s.fsf@cassou.me> Mime-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha1; protocol="application/pgp-signature"; boundary="aGaOJXZVAfIy7Tdd" Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="32117"; mail-complaints-to="usenet@ciao.gmane.io" To: emacs-devel@gnu.org Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Sat Feb 25 10:40:41 2023 Return-path: Envelope-to: ged-emacs-devel@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1pVr2n-00089N-0T for ged-emacs-devel@m.gmane-mx.org; Sat, 25 Feb 2023 10:40:41 +0100 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1pVr2V-000309-TG; Sat, 25 Feb 2023 04:40:24 -0500 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1pVr2V-000301-1E for emacs-devel@gnu.org; Sat, 25 Feb 2023 04:40:23 -0500 Original-Received: from mail.tuxteam.de ([5.199.139.25]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1pVr2T-0004a7-7A for emacs-devel@gnu.org; Sat, 25 Feb 2023 04:40:22 -0500 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=tuxteam.de; s=mail; h=From:In-Reply-To:Content-Type:MIME-Version:References:Message-ID: Subject:To:Date:Sender:Reply-To:Cc:Content-Transfer-Encoding:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Id:List-Help:List-Unsubscribe:List-Subscribe: List-Post:List-Owner:List-Archive; bh=htD3DHqPDVULkW3upkBimAYNlHSXkwmZaErR7bFWMR8=; b=FBnUFvbl3m08ir2QhgHfejhARm mwWjcunjj0itcjLDFs0H7CABLaUUYN9VaxdWXObilsN/HPLFK/ZBSCyi1I7ZG16OGYOFJP0VZ/b8J CZOgkDZEjgk9eVoSt0qJam1l/SFIQH0bsa+lfK7kCN22BiQLyBoDmQLvyFzGUGt1MrmlUBgrR38sE vmrqBM3RI2kaQ5i35PR1GdkISx9lN2wP+4dAV53/XHspYQ431AqOjIS44M3ee1VUxZg3X5tODHR4+ daLQ3uNS4zxpDdmlCLBmLJDLsMAtRxxjjWmk8o/Cr9mKLgreGwpU7HaOhThVHcIFICUrSF/0RLuim aVvFv1OQ==; Original-Received: from tomas by mail.tuxteam.de with local (Exim 4.94.2) (envelope-from ) id 1pVr2Q-0004kM-Rb for emacs-devel@gnu.org; Sat, 25 Feb 2023 10:40:18 +0100 Content-Disposition: inline In-Reply-To: <87o7pi9m9s.fsf@cassou.me> Received-SPF: pass client-ip=5.199.139.25; envelope-from=tomas@tuxteam.de; helo=mail.tuxteam.de X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: "Emacs development discussions." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Original-Sender: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Xref: news.gmane.io gmane.emacs.devel:303787 Archived-At: --aGaOJXZVAfIy7Tdd Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On Sat, Feb 25, 2023 at 09:34:07AM +0100, Damien Cassou wrote: > I tried to send the mail below to help-gnu-emacs@gnu.org (without being > subscribed) but it never reached the archive: > https://lists.gnu.org/archive/html/help-gnu-emacs/2023-02/threads.html. >=20 > Hi, >=20 > I'm wondering why the code below works but won't compile. >=20 > foo.el: > (defvar foo-var '((message "hello world"))) > =20 > (defmacro foo-macro () > `(progn > ,@foo-var)) > =20 > (defun foo-fun () > (foo-macro)) >=20 >=20 > $ emacs --batch -l foo.el --eval '(foo-fun)' > hello world >=20 > $ emacs --batch --eval '(find-file "foo.el")' --eval '(emacs-lisp-byte-co= mpile)' > In toplevel form: > foo.el:32:1: Error: Symbol=E2=80=99s value as variable is void: foo-var >=20 > Why isn't the compiler aware of the foo-var variable? Because the (dynamic) variable comes into existence later, at run time (at compile time the (defvar ...) hasn't yet been executed). You might try to wrap your defvar in an (eval-when-compile ...) form. Other more knowledgeable folks around here might chime in whether (or when) this may be a good idea or not. But the main takeaway is that compile time (and thus macro expansion time) and run time are kind of different "worlds". (See also eval-and-compile, but in this case you'll be fine with eval-when-compile, I think) Cheers --=20 t --aGaOJXZVAfIy7Tdd Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iF0EABECAB0WIQRp53liolZD6iXhAoIFyCz1etHaRgUCY/nXfAAKCRAFyCz1etHa RpE+AJ9Hf6Wsn6MJrNxinMQVWIePZIeIDgCfTdpv1ncAgBxeRzStOLT1PnblFho= =bc1Y -----END PGP SIGNATURE----- --aGaOJXZVAfIy7Tdd--