A superior implementation which does not use ‘eval’ and correctly interacts with the library system and phasing:
(define-syntax constant-expression
(syntax-rules ()
((_ expr) (let-syntax ((const (lambda (stx) #`(quote #,expr)))) const))))
I don’t think this is a very good or useful idea, though.
As Panicz says, a compiler will generally do this sort of thing automatically.
In the cases where it can’t, there are usually good reasons.
Daphne
> On 3 Aug 2025, at 03:16, James Milne <xxxxxx@gmail.com> wrote:
>
> The "constexpr" -> A call for making any arbitrary something happen at compile time, rather than runtime.
>
> This is something I've used a bit, and because it is something that a lot of other languages have adopted...
>
> Would there be any interest in making it available?
>
> One implementation and a demonstration, which is fairly portable across Schemes:(define-syntax constexpr
> (syntax-rules ()
> ((_ expr)
> (begin
> (syntax->datum
> (datum->syntax
> (quote-syntax here)
> (eval 'expr (interaction-environment))))))))
>
> (define (factorial n)
> (if (<= n 1)
> 1
> (* n (factorial (- n 1)))))
>
> (define n
> (constexpr (factorial 10)))
>