@ctfeonly

Jonathan M Davis newsgroup.d at jmdavisprog.com
Thu Dec 7 08:32:35 UTC 2017


On Thursday, December 07, 2017 07:41:18 Mike Franklin via Digitalmars-d 
wrote:
> On Thursday, 7 December 2017 at 04:45:15 UTC, Jonathan M Davis
>
> wrote:
> > The simplest way to do that is to write a unit test that uses a
> > static assertion. As I understand it, with the way CTFE works,
> > it pretty much can't know whether a function can be called at
> > compile time until it tries, but a unit test can catch it if
> > the function no longer works at compile time.
>
> Not bad, but that is swaying into the cumbersome category.  If
> that's the best we can do, a @ctfeonly attribute starts looking
> pretty good.

In the vast majority of cases, when a function is used for CTFE, it's also
used during runtime. So, in most cases, you want to ensure that a function
works both with CTFE and without, and in those cases something like
@ctfeonly wouldn't make any sense. In my experience, pretty much the only
time that something like @ctfeonly would make any sense would be with a
function for generating a string mixin. I'm sure that there are folks who
have other uses for functions that would only be used during CTFE, but
really, CTFE is designed with the idea that functions would be useable both
at runtime and at compile time, and most functions that are used during CTFE
were not designed just for CTFE. Certainly, trying to test or enforce that a
function is only used at compile time is not going to be the common case.

- Jonathan M Davis



More information about the Digitalmars-d mailing list