Expanding CTFE code during compilation

Azi Hassan azi.hassan at live.fr
Wed Jul 20 00:33:27 UTC 2022


On Tuesday, 19 July 2022 at 22:41:43 UTC, Steven Schveighoffer 
wrote:
> On 7/19/22 5:43 PM, Azi Hassan wrote:
>
>> Just in case this is a consequence of the XY problem, the 
>> reason why I'm looking for this is to make sure that the code 
>> I wrote did evaluate to what I'm expecting it to. Right now I 
>> do this with an enum assignment followed by static asserts, 
>> but I'd love it for there to be some sort of visual feedback.
>> 
>> Thanks in advance
>
>
> ```d
> template printEnum(alias x, string file = __FILE__, size_t line 
> = __LINE__)
> {
>    enum printEnum = x;
>    pragma(msg, file, "(", int(line), "): ", printEnum);
> }
>
> enum x = printEnum!(iota(1, 5).reduce!"a * b");
> ```
>
> This will spit out the file, line, and value for the 
> declaration. Assuming this is useful for your double-checking 
> you could do this, and then switch it back when you have 
> verified that it's correct.
>
> -Steve

Nice, a compile time console.log. Thanks a lot, this will come in 
handy. I wonder if it can be combined with version(debug) to only 
run the pragma line if compiled with -g, this way we can keep the 
printEnum! line as it is. Then again, the code would become 
polluted by debugging logic


More information about the Digitalmars-d-learn mailing list