new annotation or pragma to mark functions that are intended to be only used during compile time

Steven Schveighoffer schveiguy at gmail.com
Thu Feb 20 15:50:33 UTC 2025


On Thursday, 13 February 2025 at 14:56:42 UTC, Ilya wrote:
> # Proposal
>
> I'm proposing to add a new function annotation (or pragma) to 
> mark functions that should never be executed at run time. I 
> call the annotation `@ctonly` in this proposal, but I'm 
> notoriously bad at naming things, so I'm open to naming 
> suggestions.

This has been proposed before, and it doesn't require language 
changes:

```d
void foo() {
    assert(__ctfe);
}
```

Basically, this will never work at runtime, only at compile time. 
It's already used in a lot of D code for things like this.

The compiler can take this hint as "do not optimize or generate 
object code for this".

The compiler can also decide at code-generation time to have an 
error if it has tried to call the function, or maybe the mark 
gets spread to the next level up?

It can be very straightforward -- if this is not the first 
runtime statement in the function, then it doesn't get the 
benefit. It has to be this form.

-Steve


More information about the dip.ideas mailing list