How to use ".stringof" to get the value of a variable and not the name of the variable (identifier) itself?

Dennis dkorpel at gmail.com
Tue Oct 10 11:45:25 UTC 2023


On Monday, 9 October 2023 at 16:33:32 UTC, rempas wrote:
> However, in my example, "stringof" returns the character "i" 
> itself and turns that into a string instead of getting its 
> actual value (number).

The result of `.stringof` is implementation defined, it can be 
used for debugging but don't make your program's semantics depend 
on the output of it.

...

...

...That being said, this trick can be used to convert an integer 
to string at compile time:


```D
enum itoa(int i) = i.stringof;

static foreach(i; 0 .. 10) {
   mixin(create_fn!(itoa!i));
}
```

Technically not reliable, but I don't expect integers to ever get 
printed differently than a string of base 10 digits.


More information about the Digitalmars-d-learn mailing list