October 21, 2018 Can this recursive template type with named type parameters be simplified or improved? | ||||
---|---|---|---|---|
| ||||
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 |
October 22, 2018 Re: Can this recursive template type with named type parameters be simplified or improved? | ||||
---|---|---|---|---|
| ||||
Posted in reply to aliak | On Sunday, 21 October 2018 at 21:23:35 UTC, aliak wrote: > 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). > I don't know if this is at all helpful, but I spent an hour on it, so I'm going to share it: https://pastebin.com/cGZwc649 You can wrap it in a template if you want to retain the ability to pass arguments without names. Regards, Hakan |
Copyright © 1999-2021 by the D Language Foundation