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

mw m at g.c
Mon Oct 9 16:42:38 UTC 2023


use:

import std.conv;

... i.to!string ...



```
import std.stdio;
import std.conv;

static foreach(i; 0 .. 10) {
   mixin(create_fn!(i.to!string));
}

enum create_fn(string num) = `
   void function_`~ num ~`() { writeln("Hello from function `~ num 
~`!"); }
`;

void main() {
   function_9();
}

```


$ dmd -c gen_fun_i.d
$ nm gen_fun_i.o  | grep function_
0000000000000000 W _D9gen_fun_i10function_0FZv
0000000000000000 W _D9gen_fun_i10function_1FZv
0000000000000000 W _D9gen_fun_i10function_2FZv
0000000000000000 W _D9gen_fun_i10function_3FZv
0000000000000000 W _D9gen_fun_i10function_4FZv
0000000000000000 W _D9gen_fun_i10function_5FZv
0000000000000000 W _D9gen_fun_i10function_6FZv
0000000000000000 W _D9gen_fun_i10function_7FZv
0000000000000000 W _D9gen_fun_i10function_8FZv
0000000000000000 W _D9gen_fun_i10function_9FZv

$ dmd gen_fun_i.d
$ ./gen_fun_i
Hello from function 9!


On Monday, 9 October 2023 at 16:33:32 UTC, rempas wrote:
> Let's see the following example:
>
> ```d
> import std.stdio;
>
> static foreach(i; 0 .. 10) {
>   mixin(create_fn!(i.stringof));
> }
>
> enum create_fn(string num) = `
>   void function_`~ num ~`() { writeln("Hello from function `~ 
> num ~`!"); }
> `;
>
> void main() {
>   function10();
> }
> ```
>
> I'm trying to create a series of function. There will be ten of 
> them, and they will be called `function_0`, `function_1`, etc. 
> However, in my example, "stringof" returns the character "i" 
> itself and turns that into a string instead of getting its 
> actual value (number).
>
> Any ideas how I can achieve what I'm trying to achieve?




More information about the Digitalmars-d-learn mailing list