Create module-level function using mixin template?

monkyyy crazymonkyyy at gmail.com
Fri Apr 25 16:59:16 UTC 2025


On Friday, 25 April 2025 at 16:14:49 UTC, Andy Valencia wrote:
> I have a code pattern, and would like to generate rather than 
> copy/paste.  It _seems_ like mixin templates apply, but I'm not 
> having much luck.  I saw one comment that templates always 
> expand in their own context, so perhaps they're not useful for 
> generating a top-level function?
>
> Andy

its extremely unclear what your trying to do my best geuss:

```d
int func1()=>3;
int func2()=>4;

mixin template makenewfunction(alias F,int reference){
     int newfunction(int i:reference)(){
	return F()*100;
}}

mixin makenewfunction!(func1,2);
mixin makenewfunction!(func2,1);

import std;
void main(){
     newfunction!1.writeln;
     newfunction!2.writeln;
}
```

contexts airnt quite the same as unreachable, but you will need 
to have a bunch of tricks to disambiguate in practice


More information about the Digitalmars-d-learn mailing list