unofficial mirror of guile-user@gnu.org 
 help / color / mirror / Atom feed
From: Jean Abou Samra <jean@abou-samra.fr>
To: Vivien Kraus <vivien@planete-kraus.eu>, guile-user <guile-user@gnu.org>
Subject: Re: Does the eval-when example work?
Date: Wed, 19 Oct 2022 22:47:47 +0200	[thread overview]
Message-ID: <1bdfde63-f990-f3d6-5088-96b2d0fd07df@abou-samra.fr> (raw)
In-Reply-To: <d8869db4c5a3f47c70008f8b364cc3b7b58d52da.camel@planete-kraus.eu>

Le 19/10/2022 à 22:32, Vivien Kraus a écrit :
> Dear guile users,
>
> The manual, section 6.8.8, presents the eval-when form with an example:
>
> (use-modules (srfi srfi-19))
> (eval-when (expand load eval)
>    (define (date) (date->string (current-date))))
> (define-syntax %date (identifier-syntax (date)))
> (define *compilation-date* %date)
>
> I take the liberty to add:
>
> (display *compilation-date*)
> (newline)
>
> Now, when I save to test.scm and run guile -s test.scm, it gets
> compiled and it displays the current date. However, if I run this
> command a second time, the file is not recompiled, but the compilation
> date changes. Is it intended?
>
> If so, this is not exactly what I am looking for. I am looking for a
> way to run the (date) form during the compilation phase, and save the
> date to the compilation unit so that it does not change. Is this
> possible?



This code is behaving as expected. The manual's description of the example
and the naming "*compilation-date*" look buggy, however. It claims that the
code works in the REPL but not in a file, whereas the code does work in 
a file.
identifier-syntax just makes a macro that replaces an identifier with
some form. The code you quote just expands to the same as

(use-modules (srfi srfi-19))
(define (date)
   (date->string (current-date))
(define *compilation-date* (date))
(display *compilation-date*)
(newline)

On the other hand, if you define a macro that doesn't expand to the
function call "(date)" but to the result of this function call at
the time of compilation, it does work as a compilation timestamp
(and eval-when becomes really necessary).

(use-modules (srfi srfi-19))
(eval-when (expand load eval)
   (define compilation-start-date (date->string (current-date))))
(define-syntax *compilation-date*
   (lambda (sintax)
     (datum->syntax sintax compilation-start-date)))

(display *compilation-date*)
(newline)


Best,
Jean





  reply	other threads:[~2022-10-19 20:47 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-10-19 20:32 Does the eval-when example work? Vivien Kraus
2022-10-19 20:47 ` Jean Abou Samra [this message]
2022-10-22 13:26   ` Jean Abou Samra
2022-10-20  8:53 ` Maxime Devos
2022-10-21 17:41   ` Vivien Kraus

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

  List information: https://www.gnu.org/software/guile/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1bdfde63-f990-f3d6-5088-96b2d0fd07df@abou-samra.fr \
    --to=jean@abou-samra.fr \
    --cc=guile-user@gnu.org \
    --cc=vivien@planete-kraus.eu \
    /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.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).