Why can't templates use tuples for for argument types?

Reiner Pope some at address.com
Thu Jul 19 01:40:33 PDT 2007


Bill Baxter wrote:
> Jarrett Billingsley wrote:
>> "Bill Baxter" <dnewsgroup at billbaxter.com> wrote in message 
>> news:f7m9k9$es4$1 at digitalmars.com...
>>> Rather than proposing a bunch of new syntax, isn't the solution just 
>>> to make those things work as expected?  If you want at least one 
>>> thing it seems perfectly reasonable to me to do:
>>>
>>> template Foo(A, B...) {
>>>    alias Tuple!(A,B) ArgTuple;
>>>    ...
>>> }
>>
>> Well the issue is that an identifier as a template parameter already 
>> has a well-defined meaning - it's a type parameter.  Changing it to 
>> allow anything seems awfully scary, hence the suggestions for new syntax. 
> 
> 
> Yeh I meant for the A to be a type there.
> I guess I just don't understand the use case for BCS's original example:
> 
>   template Foo(A, B...) {}
> 
>   these should work
> 
>   Foo!(1, int);
>   Foo!(int, 1);
>   Foo!(Foo!(int, 1));
> 
> 
> If you don't even know what kind of entity a thing is, there's not much 
> you can do with it.
> 
> Anyway, since it's been a basically a dialogue between you and BCS so 
> far, something tells me you haven't hit a nerve with many folks.  My 
> guess is that most folks aren't clear what the motivating use case is. 
> And probably Walter isn't going to be much interested in this either 
> without good use case examples.
> 

Actually, I only saw this thread a few days ago (because I don't read 
d.learn as much as the main group), and I am surprised that so few 
people responded. To me, this is one part of a whole lot of issues with 
template parameters, which I hope will sometime be solved by a big 
reform. Otherwise, it will remain what seems today to be a collection of 
kludges. As far as I'm concerned, the problems are:

1. tuple parameters allow int[] (and double[] and double[][], etc) as 
value parameters; template value parameters don't
2. there's no equivalent to typesafe varargs: you can't say, "give me a 
type tuple" or "give me a tuple of aliases" or "give me a value tuple" 
-- you can only say "give me a tuple" and then do static asserts. I find 
this quite annoying because most of the time I know exactly which I want.
3. There's no easy way to specify "give me just one value parameter of 
any type." The closest you can get is
     template foo(T, T S) {...}
   but the instantiation requires:
     foo!(int, 5);
4. There's no way to specify what BCS mentioned: "give me just one 
parameter, of any kind". I haven't needed this yet, though.
5. You can't do proper nested tuples without a going through 
"compile-time structs".

(when thinking about template parameters in the past, I've always found 
it difficult to distinguish between "type" as in int/char/etc, and the 
"type" of a template parameter as in value/alias/type. I think "kind" is 
a good word for the latter)


I don't know what other people think of these problems, but they don't 
seem too hard to solve (mind you, I haven't worked out the details, so I 
could well be wrong). I would imagine setting up some sort of 
inheritance of template parameter kind, so that there's a base kind, 
"any", from which "value", "alias" and "type" inherit, and from the 
"value" kind, all of the regular D types also inherit.
[Additionally, you solve problem 1 above by, well, solving it.]


More information about the Digitalmars-d-learn mailing list