From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Basil Contovounesios Newsgroups: gmane.emacs.devel Subject: Re: Using the content of a dynamic variable in a macro Date: Mon, 27 Feb 2023 19:03:15 +0000 Message-ID: <87lekj53t8.fsf@tcd.ie> References: <87o7pi9m9s.fsf@cassou.me> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="11773"; mail-complaints-to="usenet@ciao.gmane.io" User-Agent: Gnus/5.13 (Gnus v5.13) Cc: emacs-devel@gnu.org To: Damien Cassou Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Mon Feb 27 20:04:13 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 1pWinF-0002s7-JU for ged-emacs-devel@m.gmane-mx.org; Mon, 27 Feb 2023 20:04:13 +0100 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1pWimZ-0006X8-4n; Mon, 27 Feb 2023 14:03:31 -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 1pWimW-0006Ws-B7 for emacs-devel@gnu.org; Mon, 27 Feb 2023 14:03:30 -0500 Original-Received: from mail-pj1-x102f.google.com ([2607:f8b0:4864:20::102f]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1pWimR-0003lA-9e for emacs-devel@gnu.org; Mon, 27 Feb 2023 14:03:26 -0500 Original-Received: by mail-pj1-x102f.google.com with SMTP id y2so7223101pjg.3 for ; Mon, 27 Feb 2023 11:03:19 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=tcd.ie; s=google21; h=content-transfer-encoding:mime-version:user-agent:message-id:date :references:in-reply-to:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=Y8/Jji0sam/lg6EIwKm0ow+ERCzxqO4qJXH0YYYhsKs=; b=EIO8SlmpkLwQkbh9xa1fwd4g/l83RISmtMyIgKXATy7nN+ptFOH3gcp5BOAU4SSKR3 ii67Up1jS8MGnLgFTzBZ0JedqS8ogyvOouakznsYl3Cs1cZYrkQeXzs0cnTigOuYmXz+ HgokimHYG5wKOvJvSmyY0ZtPrDYgsQ/mts7MSZxdnktIasbBIrqsuucYYgKOwANPXaML i/miG3NGrMwQXVMbA+JZ5SgNbp4WvpC3+epr1t4uzv8JCrkyq3UdAIyS4xALTFYtKZLD Kp1QPq8I34sV3FwfP6tpn8pOh5Mn9dXvjJfyOI27nacsd1XxGzU2teULoVEJhjzkyApo h5Ww== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:user-agent:message-id:date :references:in-reply-to:subject:cc:to:from:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=Y8/Jji0sam/lg6EIwKm0ow+ERCzxqO4qJXH0YYYhsKs=; b=Y6fonc+pEyDp7uxaPW+DkM1rzv3niLAp4nHro2b+xnJ/IqRNpI8VkJRc+Ct++aFpMM 49Y0OGQYNkcpIktGbK4lbtToFn2lutIfpNk3EB0W627+YDkRWtDRnTiN1yPRYxavlJ3s 4dnAlRszxT1nnfdYEfjfWTZIKHILxQ3uG1IMbJ85eGjbxYcL6v+Pvb7Kw3YDPxY/ng7x k6XguLrU4I2yYvnoFF1g4Ag9dvW4dyeJcgbp9Ur9mETRKxQSvQIySM0k6dQhzVAc0MkF Ls5zc4kInUYnCkgG2yqVUp9I6sse57cdNyLbtsY57v75qS6pUwuoHV0aU3EMdyRHpfg0 LYuA== X-Gm-Message-State: AO0yUKXT3KuCpLaATdjLQkWFwoXSODlA/U69YHjv/EZjx1GQRIHBhBqD nAwZklDs1x3myw5wQHt+zbhIf5GN7G2dJTyBsbA= X-Google-Smtp-Source: AK7set+bhUzVOMty0iiF6T9V3+zEQyGengUSB0gRLmEDnyI8R0J5jzf85rjA+VFs6sjXfrrU4i1S3Q== X-Received: by 2002:a05:6a20:4c94:b0:be:a9c7:5d12 with SMTP id fq20-20020a056a204c9400b000bea9c75d12mr455813pzb.18.1677524598508; Mon, 27 Feb 2023 11:03:18 -0800 (PST) Original-Received: from localhost ([109.78.133.215]) by smtp.gmail.com with ESMTPSA id u8-20020a656708000000b005004919b31dsm4342756pgf.72.2023.02.27.11.03.17 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 27 Feb 2023 11:03:17 -0800 (PST) In-Reply-To: <87o7pi9m9s.fsf@cassou.me> (Damien Cassou's message of "Sat, 25 Feb 2023 09:34:07 +0100") Received-SPF: pass client-ip=2607:f8b0:4864:20::102f; envelope-from=contovob@tcd.ie; helo=mail-pj1-x102f.google.com 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, RCVD_IN_DNSWL_NONE=-0.0001, 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:303840 Archived-At: Damien Cassou [2023-02-25 09:34 +0100] wrote: > I'm wondering why the code below works but won't compile. > > foo.el: > (defvar foo-var '((message "hello world"))) >=20=20=20 > (defmacro foo-macro () > `(progn > ,@foo-var)) >=20=20=20 > (defun foo-fun () > (foo-macro)) > > > $ emacs --batch -l foo.el --eval '(foo-fun)' > hello world > > $ 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 > > Why isn't the compiler aware of the foo-var variable? [ Lynn and Tomas have already answered this, but just to put it all together and cite the manual: ] The reason is that the byte-compiler generally avoids evaluating the source it is compiling, with the exception of forms like 'require' and 'eval-and-compile'. But at the same time, the byte-compiler needs to expand all macro calls. So in foo-fun it tries to expand the call to foo-macro, which inspects the value of foo-var, but foo-var's initial value has not yet been evaluated (by the byte-compiler, that is). To load a definition into the byte-compiler, it needs to be placed in either eval-when-compile (for things specific to the byte-compilation process, such as loading macro definitions from another library), eval-and-compile (for things that are needed at load time as well, such as defining a missing function or modifying load-path), or a separate library that is then 'require'd. 'require' is probably the least likely to confuse the reader, but is often overkill. In the example foo-var is only ever used during macroexpansion, so eval-when-compile is okay - the variable will be completely omitted from the .elc, but no load/run time code uses it anyway. But IME this is a rare use-case for global variables, which are more often also present in the resulting .elc - so eval-and-compile may be safer or more appropriate depending on the context. It is also closer to 'require'. This stuff is briefly touched on under "(elisp) Compilation Functions": --8<---------------cut here---------------start------------->8--- Be careful when writing macro calls in files that you intend to byte-compile. Since macro calls are expanded when they are compiled, the macros need to be loaded into Emacs or the byte compiler will not do the right thing. The usual way to handle this is with =E2=80=98require=E2= =80=99 forms which specify the files containing the needed macro definitions (see Named Features). Normally, the byte compiler does not evaluate the code that it is compiling, but it handles =E2=80=98require=E2=80=99 forms s= pecially, by loading the specified libraries. To avoid loading the macro definition files when someone _runs_ the compiled program, write =E2=80=98eval-when-compile=E2=80=99 around the =E2=80=98require=E2=80=99 ca= lls (see Eval During Compile). For more details, see Compiling Macros. --8<---------------cut here---------------end--------------->8--- HTH, --=20 Basil