CTFE write message to console

Carl Sturtivant sturtivant at gmail.com
Fri Apr 5 14:41:12 UTC 2024


On Friday, 5 April 2024 at 07:37:20 UTC, Paolo Invernizzi wrote:
> pragma(msg, x) ?

No.

`__ctfeWrite(x)` is executed inside an executing function like 
any other statement in it, and can have an argument `x` computed 
during that execution.

It is defined to output the computed text `x` to stderr when the 
function it is a part of is called as CTFE by the compiler and to 
be a no-op if that function is called at run time in the compiled 
executable.

So it is a replacement for `std.stdio.write` in a CTFE function, 
handy among other things for reporting what's going on during the 
execution of that function by the compiler.

`pragma(msg, x)` works during its *compilation*, so putting it as 
a line in a CTFE-called function would not execute it when the 
function is called by the compiler. That being the case, `x` may 
not be a value computed when that function executes. Such a value 
is "not available at compile time", only at CTFE run time. The 
CTFE function is not running when it is being compiled.

It is possible that `x` in `pragma(msg, x)` be computed with a 
call to a function as its return value, i.e. computed by CTFE, 
but that call will be made when `pragma(msg, x)` is compiled, and 
is a part of compiling it. Only when the function has returned 
producing `x` does the `pragma(msg, x)` do its work.


More information about the Digitalmars-d-learn mailing list