Thread overview
How do I specify a variadic template constraint on all the whole tuple?
Jul 18, 2013
Atila Neves
Jul 18, 2013
Andrej Mitrovic
Jul 18, 2013
Atila Neves
July 18, 2013
http://stackoverflow.com/questions/17724479/in-d-how-do-i-specify-a-variadic-template-constraint-on-all-the-whole-tuple

Posted on stackoverflow first since I think it's better for the language to get traction there. For completeness sake, posting the text of the question here as well:



I'm writing 2 overloads of a function, both of them with variadic template compile-time parameters. One should take symbols as the templates, the other strings. I want to constrain the template instantiation to these two cases. The best I came up with was this:

bool func(SYMBOLS...)() if(!is(typeof(SYMBOLS[0]) == string)) {
}

and

bool func(STRINGS...)() if(is(typeof(STRINGS[0]) == string)) {
}

Obviously this only checks the first template parameter, and while it works given the code I've written so far, I wish I could say "only for all strings" and "only for not all strings". Is there a way?

Thanks in advance.
July 18, 2013
On Thursday, 18 July 2013 at 13:18:29 UTC, Atila Neves wrote:
> bool func(SYMBOLS...)() if(!is(typeof(SYMBOLS[0]) == string)) {
> }
> and
>
> bool func(STRINGS...)() if(is(typeof(STRINGS[0]) == string)) {
> }

Here you go:

-----
import std.string;
import std.traits;
import std.typetuple;
import std.functional;

bool func(Symbols...)(Symbols symbols)
    if (!anySatisfy!(isSomeString, Symbols))
{
    return true;
}

bool func(Strings...)(Strings strings)
    if (allSatisfy!(isSomeString, Strings))
{
    return true;
}

void main()
{
    func("1", "2");
    func(1, 2);
    static assert(!__traits(compiles, func(1, "2")));
}
-----

And remember you have to actually list the runtime arguments after the type arguments (e.g. 'Strings strings'), otherwise the template won't match.
July 18, 2013
Thanks! I had to change allSatistfy!(isSomeString, Symbols) to allSatistfy!(isSomeString, typeof(Symbols)) but it works.

I don't use the same types as runtimne arguments actually. You might see why if/when I finish this particular project and announce it.

Shall I answer my own question on stackoverflow?

Atila

On Thursday, 18 July 2013 at 13:25:55 UTC, Andrej Mitrovic wrote:
> On Thursday, 18 July 2013 at 13:18:29 UTC, Atila Neves wrote:
>> bool func(SYMBOLS...)() if(!is(typeof(SYMBOLS[0]) == string)) {
>> }
>> and
>>
>> bool func(STRINGS...)() if(is(typeof(STRINGS[0]) == string)) {
>> }
>
> Here you go:
>
> -----
> import std.string;
> import std.traits;
> import std.typetuple;
> import std.functional;
>
> bool func(Symbols...)(Symbols symbols)
>     if (!anySatisfy!(isSomeString, Symbols))
> {
>     return true;
> }
>
> bool func(Strings...)(Strings strings)
>     if (allSatisfy!(isSomeString, Strings))
> {
>     return true;
> }
>
> void main()
> {
>     func("1", "2");
>     func(1, 2);
>     static assert(!__traits(compiles, func(1, "2")));
> }
> -----
>
> And remember you have to actually list the runtime arguments after the type arguments (e.g. 'Strings strings'), otherwise the template won't match.