guile-user
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Does the eval-when example work?


From: Jean Abou Samra
Subject: Re: Does the eval-when example work?
Date: Wed, 19 Oct 2022 22:47:47 +0200
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.3.1

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 via email to

[Prev in Thread] Current Thread [Next in Thread]