Variadic grouping

JS js.mdnq at gmail.com
Mon Jul 29 06:36:36 PDT 2013


On Monday, 29 July 2013 at 13:30:58 UTC, bearophile wrote:
> JS:
>
>> I doubt such a feature will ever get added but who knows...
>
> It seems a cute idea, but why don't you show two or more very 
> different use cases? (Asking for a feature without showing use 
> cases is not so good.)
>
> Bye,
> bearophile

The usefulness should be obvious and I seriously doubt if someone 
thinks it is not then any example I could give would convince 
them otherwise.

It came up for me trying to write a ternary if to use.


struct tVariadicSplit { }
template tuple(args...) { alias tuple = args; }
template tMin(alias a, alias b)
{
	static if (a < b) alias tMin = a; else alias tMin = b;
}

template tIf(alias cond, args...)
{
	enum sp = std.typetuple.staticIndexOf!(tVariadicSplit, args);
	static if (sp < 0) enum spp = args.length; else enum spp = sp;
     static if (cond) alias tIf = args[0..tMin!($, spp)];	else 
alias tIf = args[tMin!($,spp+1)..$];
}


I have to use tVariadicSplit to split the grouping(sure I could 
reduce the symbol name size, which I have done).

Being able to write this as

template tIf(alias cond, tArgs..., fArgs...)
{
     static if (cond)
         alias tIf = tArgs;
     else
         alias tIf = fArgs;
}

Would be much much more elegant.



More information about the Digitalmars-d mailing list