Mixin and imports

jmh530 john.michael.hall at gmail.com
Mon Jun 8 02:55:25 UTC 2020


In the code below, foo!fabs compiles without issue, but 
foo!"fabs" does not because the import is not available in the 
string mixin. If I move the import to the top of the module, then 
it works. However, then if I move foo to another module, then it 
will no longer compile since it is in different modules.

Is there any way I can make sure the mixin knows about the import?

I am just using fabs as an example. Ideally, I would want to use 
any function from any other module. I can figure out how to do 
it, for instance, if the only functions that were allowed were 
those in one module, like std.math.

```
double foo(alias f)(double x) {
     return f(x);
}

template foo(string f) {
     mixin("alias foo = .foo!(" ~ f ~ ");");
}

void main() {
     import std.math: fabs;

     double x = 2.0;
     double y = foo!fabs(x);
     double z = foo!"fabs"(x);
}
```


More information about the Digitalmars-d-learn mailing list