Request for Comment assert(__ctfe)

Johannes Pfau nospam at example.com
Wed Apr 8 20:12:09 UTC 2020


Am Wed, 08 Apr 2020 19:16:15 +0000 schrieb Jonathan Marler:

> On Wednesday, 8 April 2020 at 09:48:25 UTC, Stefan Koch wrote:
>> On Wednesday, 8 April 2020 at 09:11:45 UTC, Dennis wrote:
>>> On Wednesday, 8 April 2020 at 08:42:52 UTC, Stefan Koch wrote:
>>>> When existing the language is perfectly able to express (only runs at
>>>> ctfe).
>>>
>>> assert(__ctfe) does not express that you STATICALLY know it only runs
>>> at ctfe, it is a RUNTIME error and it is impossible to statically
>>> decide whether the function may actually get called outside ctfe
>>> context.
>>
>> The only reason why I used assert (__ctfe) not static assert (__ctfe)
>> is because that's impossible to express.
>> since __ctfe is a magic runtime variable that's hardwired to be zero in
>> the non-ctfe case.
> 
> This may be a naive suggestion, but how hard would it be to make
> version(ctfe) work?
> 
> version (ctfe) string generateMixin(...) { ... }
> 
> Using version means you would be able to know earlier whether a function
> is ctfe-only, but I'm not sure how you could propogate that information
> through the rest of semantic-analysis and what effects that could have.

I'd guess very hard. To be consistent with the rest of the language, you 
then have to support this:

void foo()
{
    version (ctfe)
    {
    }
    else
    {
    }
}

You can end up with two completely different function implementations. 
And you can do this at top-level as well, where you have the same function 
in version(ctfe) and in the else block.


Maybe you can somehow cheat and simplify this. But in the most general 
case, you'd have to keep two versions of the AST of each function.

-- 
Johannes


More information about the Digitalmars-d mailing list