Trying to understand map being a template
Paul Backus
snarwin at gmail.com
Sat Jan 6 17:57:06 UTC 2024
On Friday, 5 January 2024 at 20:41:53 UTC, Noé Falzon wrote:
> In fact, how can the template be instantiated at all in the
> following example, where no functions can possibly be known at
> compile time:
>
> ```
> auto do_random_map(int delegate(int)[] funcs, int[] values)
> {
> auto func = funcs.choice;
> return values.map!func;
> }
> ```
>
> Thank you for the insights!
It works for the same reason this example works:
```d
void printVar(alias var)()
{
import std.stdio;
writeln(__traits(identifier, var), " = ", var);
}
void main()
{
int x = 123;
int y = 456;
printVar!x; // x = 123
printVar!y; // y = 456
x = 789;
printVar!x; // x = 789
}
```
More information about the Digitalmars-d-learn
mailing list