Thread overview
Question about tuple as template parameter
Apr 22, 2008
Max Samukha
Apr 22, 2008
Bill Baxter
Apr 23, 2008
Max Samukha
April 22, 2008
Why the compiler chooses the template with tuple paramer? Isn't the template with type parameter more 'specialized'?

template Foo(T)
{
	pragma(msg, "Type");
}

template Foo(TT...)
{
	pragma(msg, "Tuple");
}

alias Foo!(int) foo;
----
Outputs: Tuple
April 22, 2008
Max Samukha wrote:
> Why the compiler chooses the template with tuple paramer? Isn't the
> template with type parameter more 'specialized'?
> 
> template Foo(T)
> {
> 	pragma(msg, "Type");
> }
> 
> template Foo(TT...)
> {
> 	pragma(msg, "Tuple");
> }
> 
> alias Foo!(int) foo;
> ----
> Outputs: Tuple

I don't think the spec makes any promises about that sort of thing. Generally speaking you're on shaky ground any time you try to overload templates in D.

Can you not put a "static if(TT.length==1)" in the tuple version?

Also you could try making the tuple one be

    template Foo(T0,T1,TN...) {
          alias Tuple!(T0,T1,TN) TT;
    }

So that it takes a min of 2 args to differentiate.  And then add a zero arg version if you need that too.

But I think Walter's idea with templates is that to make it simpler any ambiguity should just be an error.  So that you don't end up with a bunch of crazy rules that no two compilers implement quite the same.

--bb
April 23, 2008
On Tue, 22 Apr 2008 20:05:21 +0900, Bill Baxter <dnewsgroup@billbaxter.com> wrote:

>Max Samukha wrote:
>> Why the compiler chooses the template with tuple paramer? Isn't the template with type parameter more 'specialized'?
>> 
>> template Foo(T)
>> {
>> 	pragma(msg, "Type");
>> }
>> 
>> template Foo(TT...)
>> {
>> 	pragma(msg, "Tuple");
>> }
>> 
>> alias Foo!(int) foo;
>> ----
>> Outputs: Tuple
>
>I don't think the spec makes any promises about that sort of thing. Generally speaking you're on shaky ground any time you try to overload templates in D.
>
>Can you not put a "static if(TT.length==1)" in the tuple version?
>
>Also you could try making the tuple one be
>
>     template Foo(T0,T1,TN...) {
>           alias Tuple!(T0,T1,TN) TT;
>     }
>
>So that it takes a min of 2 args to differentiate.  And then add a zero arg version if you need that too.

Yeah, I did something similar.
>
>But I think Walter's idea with templates is that to make it simpler any ambiguity should just be an error.  So that you don't end up with a bunch of crazy rules that no two compilers implement quite the same.
>
>--bb

Agree that this should probably result in an error. There is also an inconsistency in the current implementation (dmd 2.012, http://d.puremagic.com/issues/show_bug.cgi?id=2025).