Variadic grouping

bearophile bearophileHUGS at lycos.com
Mon Jul 29 06:59:53 PDT 2013


JS:

> 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's not obvious for me :-) Explaining the "obvious" is sometimes 
necessary.


> 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)..$];
> }

(In your code I suggest to put a newline after each semicolon).

This is one use case, to implement a static ternary operator with 
multiple arguments. (But having multiple arguments is not so 
common).

A possible static ternary operator syntax:

enum foo = ct_cond !? Foo!5 : Bar!6;

But in my opinion the need for it is not strong enough, better to 
keep the language simpler.

Do you have a second use case?

Bye,
bearophile


More information about the Digitalmars-d mailing list