constexpr James Milne (03 Aug 2025 01:17 UTC)
Re: constexpr Panicz Maciej Godek (03 Aug 2025 06:09 UTC)
Re: constexpr Daphne Preston-Kendal (03 Aug 2025 09:34 UTC)
Re: constexpr Marc Nieper-Wißkirchen (03 Aug 2025 13:57 UTC)

Re: constexpr Daphne Preston-Kendal 03 Aug 2025 09:33 UTC

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)))
>