CTFE write message to console

Salih Dincer salihdb at hotmail.com
Sat Apr 6 01:27:32 UTC 2024


On Friday, 5 April 2024 at 14:41:12 UTC, Carl Sturtivant wrote:
> 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.

This works:
```d

alias E = double;
auto r = 0.iota!E(1,.05)

static this()
{
  alias ranges = imported!"std.meta".AliasSeq!(isRandomAccessRange,
  isForwardRange, isInputRange, isOutputRange, 
isBidirectionalRange);
  static foreach(R; ranges) pragma(msg, R!(typeof(r), E));
}

void main() {}
```
SDB at 79


More information about the Digitalmars-d-learn mailing list