digitalmars.D.learn - CTFE write message to console
- Carl Sturtivant (3/3) Apr 04 I'm writing CTFE on Windows, latest DMD compiler. How should I
- Richard (Rikki) Andrew Cattermole (10/13) Apr 04 ```d
- Carl Sturtivant (5/14) Apr 04 That's ... unfortunate.
- Richard (Rikki) Andrew Cattermole (4/28) Apr 04 Ah yes, I forgot about that particular thing, doesn't see much use as
- Carl Sturtivant (17/20) Apr 04 ```D
- Richard (Rikki) Andrew Cattermole (4/4) Apr 04 Oh hey!
- Carl Sturtivant (5/10) Apr 04 Wow! Happy that's in. It was a bit mysterious, but now that
- Salih Dincer (4/9) Apr 04 Good news, thanks...
- Paolo Invernizzi (2/24) Apr 05 pragma(msg, x) ?
- Carl Sturtivant (23/24) Apr 05 No.
- Salih Dincer (15/40) Apr 05 This works:
I'm writing CTFE on Windows, latest DMD compiler. How should I write a message to the console (stderr) from a CTFE function call during compilation?
Apr 04
On 05/04/2024 2:54 AM, Carl Sturtivant wrote:I'm writing CTFE on Windows, latest DMD compiler. How should I write a message to the console (stderr) from a CTFE function call during compilation?```d static assert(0, "message"); ``` Or if it is known to be CTFE'd ```d assert(0, "message"); ``` Just a warning, its a one time use only for both. No other way to do it.
Apr 04
On Thursday, 4 April 2024 at 14:06:19 UTC, Richard (Rikki) Andrew Cattermole wrote:```d static assert(0, "message"); ``` Or if it is known to be CTFE'd ```d assert(0, "message"); ``` Just a warning, its a one time use only for both. No other way to do it.That's ... unfortunate. Some search of the forum led me to some [decade plus old discussion](https://forum.dlang.org/post/j1n1m2$24p0$1 digitalmars.com) of a possible CTFE writeln function that would be a no-op at runtime, which to my surprise led me to find [core_builtins.__ctfeWrite](https://dlang.org/phobos/core_builtin the console. Given your remarks I suppose I should have expected this.
Apr 04
On 05/04/2024 4:04 AM, Carl Sturtivant wrote:On Thursday, 4 April 2024 at 14:06:19 UTC, Richard (Rikki) Andrew Cattermole wrote:Ah yes, I forgot about that particular thing, doesn't see much use as far as I'm aware. It should be working though.```d static assert(0, "message"); ``` Or if it is known to be CTFE'd ```d assert(0, "message"); ``` Just a warning, its a one time use only for both. No other way to do it.That's ... unfortunate. Some search of the forum led me to some [decade plus old discussion](https://forum.dlang.org/post/j1n1m2$24p0$1 digitalmars.com) of a possible CTFE writeln function that would be a no-op at runtime, which to my surprise led me to find [core_builtins.__ctfeWrite](https://dlang.org/phobos/core_builtin the console. Given your remarks I suppose I should have expected this.
Apr 04
On Thursday, 4 April 2024 at 15:07:21 UTC, Richard (Rikki) Andrew Cattermole wrote:Ah yes, I forgot about that particular thing, doesn't see much use as far as I'm aware. It should be working though.```D enum X = computeX("A message"); string computeX(string msg) { auto s = "CTFE msg: "; auto x = imported!"std.format".format("%s%s\n", s, msg); __ctfeWrite(x); return x; } void main() { import std.stdio; writeln(X); } ``` Produces no output on compilation, and writes out `CTFE msg: A message` when run.
Apr 04
Oh hey! https://github.com/dlang/dmd/pull/16250 It was implemented literally 2 weeks ago! Nightly should have it https://github.com/dlang/dmd/releases/tag/nightly
Apr 04
On Thursday, 4 April 2024 at 15:47:53 UTC, Richard (Rikki) Andrew Cattermole wrote:Oh hey! https://github.com/dlang/dmd/pull/16250 It was implemented literally 2 weeks ago! Nightly should have it https://github.com/dlang/dmd/releases/tag/nightlyWow! Happy that's in. It was a bit mysterious, but now that request says it was never implemented. Until now. Just what I want!
Apr 04
On Thursday, 4 April 2024 at 15:47:53 UTC, Richard (Rikki) Andrew Cattermole wrote:Oh hey! https://github.com/dlang/dmd/pull/16250 It was implemented literally 2 weeks ago! Nightly should have it https://github.com/dlang/dmd/releases/tag/nightlyGood news, thanks... SDB 79
Apr 04
On Thursday, 4 April 2024 at 15:43:55 UTC, Carl Sturtivant wrote:On Thursday, 4 April 2024 at 15:07:21 UTC, Richard (Rikki) Andrew Cattermole wrote:pragma(msg, x) ?Ah yes, I forgot about that particular thing, doesn't see much use as far as I'm aware. It should be working though.```D enum X = computeX("A message"); string computeX(string msg) { auto s = "CTFE msg: "; auto x = imported!"std.format".format("%s%s\n", s, msg); __ctfeWrite(x); return x; } void main() { import std.stdio; writeln(X); } ``` Produces no output on compilation, and writes out `CTFE msg: A message` when run.
Apr 05
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.
Apr 05
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: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 79pragma(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.
Apr 05