Determine if CTFE or RT

Martin Tschierschke mt at smartdolphin.de
Mon Jun 25 09:36:45 UTC 2018


On Monday, 25 June 2018 at 08:05:53 UTC, Mr.Bingo wrote:
> On Monday, 25 June 2018 at 07:02:24 UTC, Jonathan M Davis wrote:
>> On Monday, June 25, 2018 05:47:30 Mr.Bingo via 
>> Digitalmars-d-learn wrote:
>>> The problem then, if D can't arbitrarily use ctfe, means that 
>>> there should be a way to force ctfe optionally!
>>
>> If you want to use CTFE, then give an enum the value of the 
>> expression you want calculated. If you want to do it in place, 
>> then use a template such as
>>
>> template ctfe(alias exp)
>> {
>>     enum ctfe = exp;
>> }
>>
>> so that you get stuff like func(ctfe!(foo(42))). I would be 
>> extremely surprised if the compiler is ever changed to just 
>> try CTFE just in case it will work as an optimization. That 
>> would make it harder for the programmer to understand what's 
>> going on, and it would balloon compilation times. If you want 
>> to write up a DIP on the topic and argue for rules on how CTFE 
>> could and should function with the compiler deciding to try 
>> CTFE on some basis rather than it only being done when it must 
>> be done, then you're free to do so.
>>
>> https://github.com/dlang/DIPs
>>
>> But I expect that you will be sorely disappointed if you ever 
>> expect the compiler to start doing CTFE as an optimization. 
>> It's trivial to trigger it explicitly on your own, and 
>> compilation time is valued far too much to waste it on 
>> attempting CTFE when in the vast majority of cases, it's going 
>> to fail. And it's worked quite well thus far to have it work 
>> only cases when it's actually needed - especially with how 
>> easy it is to make arbitrary code run during CTFE simply by 
>> doing something like using an enum.
>>
>> - Jonathan M Davis
>
> You still don't get it!
>
> It is not trivial! It is impossible to trigger it! You are 
> focused far too much on the optimization side when it is only 
> an application that takes advantage of the ability for rtfe to 
> become ctfe when told, if it is possible.
>
> I don't know how to make this any simpler, sorry... I guess 
> we'll end it here.

I am not sure that I understood it right, but there is a way to 
detect the status of a parameter:

My question was different, but I wished to get a ctRegex! or 
regex used depending on the expression:

  import std.regex:replaceAll,ctRegex,regex;

  auto reg(alias var)(){
        static if (__traits(compiles, {enum ctfeFmt = var;}) ){
                 // "Promotion" to compile time value
                 enum ctfeReg =  var ;
                 pragma(msg, "ctRegex used");
                 return(ctRegex!ctfeReg);

        }else{
                 return(regex(var));
                 pragma(msg,"regex used");
                 }
        }
}
So now I can always use reg!("....") and let the compiler decide.

To speed up compilation I made an additional switch, that when 
using DMD (for development)
alway the runtime version is used.

The trick is to use the alias var in the declaration and check if 
it can be assigned to enum.
The only thing is now, that you now always use the !() compile 
time parameter to call the function. Even, when in the end is 
translated to an runtime call.

reg!("....") and not reg("...").







More information about the Digitalmars-d-learn mailing list