www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Create module-level function using mixin template?

reply Andy Valencia <dont spam.me> writes:
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

```d
bool bigtest(in string s) {
     return true;
}

bool test1(in char c) {
     return false;
}
bool test2(in char c) {
     return true;
}

mixin template MyFunc(alias fn) {
     bool fn(in string s) {
         if (!bigtest(s)) {
             return false;
         }
         foreach(c; s) {
             if (!fn(c)) {
                 return false;
             }
         }
         return true;
     }
}

mixin MyFunc!test1;
mixin MyFunc!test2;

int main() {
     if (!test1("Hello, world")) {
         return(1);
     }
     if (!test2("Hello, world")) {
         return(1);
     }
     return(0);
}
```
Apr 25
parent reply monkyyy <crazymonkyyy gmail.com> writes:
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
Apr 25
parent reply Andy Valencia <dont spam.me> writes:
On Friday, 25 April 2025 at 16:59:16 UTC, monkyyy wrote:
 its extremely unclear what your trying to do my best geuss:
I want to use a mixin template to generate a top-level function. Like, is there a variant of the following which makes a function named "foo1" available? Andy ```d mixin template Foo(alias fn) { int fn() { return 1; } } mixin Foo!foo1; void main() { import std.stdio : writeln; writeln(foo1()); } ```
Apr 25
next sibling parent =?UTF-8?Q?Ali_=C3=87ehreli?= <acehreli yahoo.com> writes:
On 4/25/25 10:24 AM, Andy Valencia wrote:
 On Friday, 25 April 2025 at 16:59:16 UTC, monkyyy wrote:
 its extremely unclear what your trying to do my best geuss:
I want to use a mixin template to generate a top-level function. Like, is there a variant of the following which makes a function named "foo1" available? Andy ```d mixin template Foo(alias fn) {     int fn() { return 1; } } mixin Foo!foo1; void main() {     import std.stdio : writeln;     writeln(foo1()); } ```
I think an 'alias' template parameter must refer to an existing symbol, which you don't have. Although I've been missing a feature of "string parameters without quotes", here is a solution: mixin template Foo(string fn, int i) { int Foo() { return i; } mixin ("alias " ~ fn ~ " = Foo;"); } mixin Foo!("foo1", 1); mixin Foo!("foo2", 2); void main() { import std.stdio : writeln; writeln(foo1()); writeln(foo2()); } Ali
Apr 25
prev sibling next sibling parent "H. S. Teoh" <hsteoh qfbox.info> writes:
On Fri, Apr 25, 2025 at 05:24:18PM +0000, Andy Valencia via Digitalmars-d-learn
wrote:
 On Friday, 25 April 2025 at 16:59:16 UTC, monkyyy wrote:
 its extremely unclear what your trying to do my best geuss:
I want to use a mixin template to generate a top-level function.
Your best bet actually is to use a string mixin. The quoted token string can help make the generating code more readable. For example: ```d string makeFunc(string name) { return q{ int }~name~q{() { return 1; } }; } mixin(makeFunc("myfunc")); void main() { assert(myfunc() == 1); } ``` T -- I see that you JS got Bach.
Apr 25
prev sibling parent monkyyy <crazymonkyyy gmail.com> writes:
On Friday, 25 April 2025 at 17:24:18 UTC, Andy Valencia wrote:
 On Friday, 25 April 2025 at 16:59:16 UTC, monkyyy wrote:
 its extremely unclear what your trying to do my best geuss:
I want to use a mixin template to generate a top-level function. Like, is there a variant of the following which makes a function named "foo1" available? Andy ```d mixin template Foo(alias fn) { int fn() { return 1; } } mixin Foo!foo1; void main() { import std.stdio : writeln; writeln(foo1()); } ```
If we ever get string mixins of identifiers maybe, but as is it wont go that well like you have the identifier at programing time, use an alias `alias foo1=Foo(...)` if you need a list, use an int and specialization, you can then access it with `static foreach(I;0..N){` if you have arguements, use a standard template if you need to use features from a struct; use propertys of the struct in line of a meta program
Apr 25