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

rempas rempas at tutanota.com
Tue Oct 10 13:51:01 UTC 2023


On Monday, 9 October 2023 at 22:49:11 UTC, Salih Dincer wrote:
> Great masters generally warn to stay away from stringof. Please 
> do not use it as much as possible. The following code snippet 
> will be useful to you:
>
> ```d
> alias CN = __traits(allMembers, CardinalNumbers);
>
> static foreach(i; CN)
> {
>   mixin(create_fn!(i[1]));
> }
>
> enum create_fn(char num) = `
>   auto function_`~ num ~`()
>     => "Hello from function `~ num ~`!";
> `;
>
> enum CardinalNumbers
> {
>   n0, n1, n2, n3, n4, n5, n6, n7, n8, n9
> }
>
> void main()
> {
>   assert(function_9() == "Hello from function 9!");
> }
> ```
>
> SDB at 79

Thank you so much! This will do the trick. Have a beautiful day!


More information about the Digitalmars-d-learn mailing list