Stefan Koch: New CTFE fix

Johnson Jones via Digitalmars-d digitalmars-d at puremagic.com
Mon Aug 14 15:51:04 PDT 2017


Hi Stefan,

I have a suggestion/request for you for newCTFE:

string do()
{
    string x;
    x = "adsf";
    pragma(msg, x);
    return x;
}

fails because the compiler believes that x is not known at 
compile time. It obviously is when do is ran as a ctfe. This 
makes some types of programming difficult to debug because we 
have to duplicate any instance of "do" and cannot output 
intermediate values.

But, how can we make this actually work?

One way may be to treat all strings as enums internally and allow 
normal string operations on them, effectively creating a 
string_enum type.

In fact, maybe a new type would be the easiest way. string_enum 
is a string at runtime but an enum with string operations at 
compile time. (for memory efficiency they could be more like 
string builders)


Another possible way is for the compiler to emit the pragma(msg, 
x) as sort of writelns. i.e., ctfe_writeln, which if I recall, 
hasn't been added yet.

Then we end up something like

string do()
{
    string x;
    x = "adsf";
    ctfe_writeln(x);
    return x;
}

which either could immediately display x if possible, or the 
compiler could collect all output then display after the function 
is called(issues are that errors might break the output).

I'm sure other solutions are possible. pragma(msg, ) could act 
different depending on if it is use on a variable that is used 
inside a ctfe vs a compile time "variable" used inside a ctfe.

Maybe your newCTFE already has the ability to do this or make it 
easy? It would surely help me because I tend to use a lot of 
mixins and generally cannot easily output the results for 
debugging purposes(and since mixins already are nearly impossible 
to debug like normal code, we need something useful to help us 
out).





More information about the Digitalmars-d mailing list