How in the name of D do you deal with more than one optional template parameter?

aliak something at something.com
Wed Oct 31 13:54:23 UTC 2018


On Tuesday, 30 October 2018 at 21:19:13 UTC, H. S. Teoh wrote:
> On Tue, Oct 30, 2018 at 08:46:31PM +0000, aliak via 
> Digitalmars-d-learn wrote:
>> [...]
>
> 	struct C(Args...)
> 	if ((rest.length == 2 && is(Args[0] == B!C, C) && is(Args[1] 
> == string)) ||
> 	    (rest.length == 1 && (is(Args[0] == B!C, C) || is(Args[0] 
> == string))))
> 	{
> 		...
> 	}
>

Hah! Yep. That.

>
> [...]
>> [...]
>
> This is unnecessary.  Why not just use `void` directly?

Ah true, yes brain fart on my part.

I think it was left overs from a scenario where I didn't want to 
do this:

static if (!is(T == void)) {
   T variable;
}

Because a Void struct would be declared as well. But then one 
realises that everywhere you want to use "variable" you need to 
guard it with a static if so meh.

>
> 	struct C(string name, T, string desc)
> 		if (is(T : B!C, C) || is(T == void))
> 	{
> 		...
> 	}
>
>
>> [...]
> [...]
>
> You can also use default parameters for templates:
>
> 	struct C(string name, T, string desc = null)
> 	{
> 		...
> 	}
>
> then you just need one more alias for the string-only variant:
>
> 	alias C(string name, string desc) = C!(name, void, desc);
>
> Note that this syntax is much shorter than the full eponymous 
> template syntax, which makes it easier to deal with longer 
> parameter lists:

Ah nice! Yes that helps indeed :)

>
> 	struct ManyParams(A a, B b=defaultB, C c=defaultC, D 
> d=defaultD)
> 	{
> 		...
> 	}
> 	alias ManyParams(A a, C c, D d=defaultD) = ManyParams!(a, 
> null, c, d);
> 	alias ManyParams(A a, D d=defaultD) = ManyParams!(a, void, 
> void, d);
>
> And so on.
>
> And there's probably a way to auto-generate those alias 
> templates if you anticipate needing to do this many times. (Or 
> if you're insane and want to let the user specify different 
> arguments in any order.) Mixins and CTFE codegen FTW!  :-D

Did I hear "specify different arguments in any order"? -> 
https://forum.dlang.org/thread/cjbqrpbpymwayzcgcrjy@forum.dlang.org.

:p

Cheers,
- Ali


>
>
> T



More information about the Digitalmars-d-learn mailing list