Can this recursive template type with named type parameters be simplified or improved?

aliak something at something.com
Sun Oct 21 21:23:35 UTC 2018


Hi, I'm playing around with a recursive template type that allows 
for named template parameters. The problem is that it requires a 
lot of repetition and becomes more error prone as the number of 
named arguments increase. So

1) Any ideas on how to make it less error prone? less repetition? 
a better way to do this?
2) Right now I can only have named optional parameters. Any ideas 
on how to make named required parameters (e.g. the first type 
parameter of Type).

Here's an example of a type that has one required parameter and 4 
optional named parameters:

private struct TypeImpl(
     T,
     string _arg0 = null,
     int _arg1 = 0,
     float _arg2 = 0,
     alias _arg3 = null,
) {
     alias Arg0 = _arg0;
     alias Arg1 = _arg1;
     alias Arg2 = _arg2;
     alias Arg3 = _arg3;
     public alias arg0(string value) = TypeImpl!(T, value, Arg1, 
Arg2, Arg3);
     public alias arg1(int value) = TypeImpl!(T, Arg0, value, 
Arg2, Arg3);
     public alias arg2(float value) = TypeImpl!(T, Arg0, Arg1, 
value, Arg3);
     public static template arg3(alias value) {
         alias arg3 = TypeImpl!(T, Arg0, Arg1, Arg2, value);
     }
}

public template Type(T) {
     alias Type = TypeImpl!(T);
}

void main() {
     void fun() {}
     alias U = Type!int
         .arg0!"string"
         .arg1!3
         .arg3!fun;
     pragma(msg, U);
}

Cheers,
- Ali



More information about the Digitalmars-d-learn mailing list