Thread overview | ||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
July 16, 2007 Why can't templates use tuples for for argument types? | ||||
---|---|---|---|---|
| ||||
|template Types(A...) |{ | template Values(A a) | { | } |} | |alias Types!(int, bool, int[]).Values!(1,true,[1,2,3]) bob; |
July 16, 2007 Re: Why can't templates use tuples for for argument types? | ||||
---|---|---|---|---|
| ||||
Posted in reply to BCS | BCS wrote: > |template Types(A...) > |{ > | template Values(A a) > | { > | } > |} > | > |alias Types!(int, bool, int[]).Values!(1,true,[1,2,3]) bob; Probably because the compiler does not unfold the tuple. I've filed a bug about instantiating an array literal from a tuple. It has a similar problem: alias Tuple!(1,2,3) a; int[] foo = [ a ]; // does not work, but int[] foo = [ a[0], a[1], a[2] ]; // does Here you can also do > |template Types(A...) > |{ > | template Values(A[0] a, A[1] b, A[2] c) > | { > | } > |} > | > |alias Types!(int, bool, int[]).Values!(1,true,[1,2,3]) bob; |
July 16, 2007 Re: Why can't templates use tuples for for argument types? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jari-Matti Mäkelä | Reply to Jari-Matti Mäkelä,
> Probably because the compiler does not unfold the tuple. I've filed a
> bug about instantiating an array literal from a tuple. It has a
> similar problem:
>
> alias Tuple!(1,2,3) a;
>
> int[] foo = [ a ]; // does not work, but
> int[] foo = [ a[0], a[1], a[2] ]; // does
So you are saying it's a bug?
In that case, what is the bug number so I can add some more comments?
|
July 17, 2007 Re: Why can't templates use tuples for for argument types? | ||||
---|---|---|---|---|
| ||||
Posted in reply to BCS | BCS wrote: > Reply to Jari-Matti Mäkelä, > >> Probably because the compiler does not unfold the tuple. I've filed a bug about instantiating an array literal from a tuple. It has a similar problem: >> >> alias Tuple!(1,2,3) a; >> >> int[] foo = [ a ]; // does not work, but >> int[] foo = [ a[0], a[1], a[2] ]; // does > > So you are saying it's a bug? > > In that case, what is the bug number so I can add some more comments? I think the first class tuples -bug covers this too: http://d.puremagic.com/issues/show_bug.cgi?id=1293 If you feel it's worth a new bug report, please do as you wish. I have to admit the situation is a bit unorthogonal - for example these work too: alias Tuple(int,int) a; void foo(a b) {} void bar() { a b; } But template baz(a b) {} doesn't. |
July 17, 2007 Re: Why can't templates use tuples for for argument types? | ||||
---|---|---|---|---|
| ||||
Posted in reply to BCS | BCS wrote:
> |template Types(A...)
> |{
> | template Values(A a)
> | {
> | }
> |}
> |
> |alias Types!(int, bool, int[]).Values!(1,true,[1,2,3]) bob;
int [] is not an allowed as a template value parameter. Only char[], wchar[], dchar[], integers, and floating-point types can be template value parameters.
(In C++, only integers are allowed).
|
July 17, 2007 Re: Why can't templates use tuples for for argument types? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jari-Matti Mäkelä | Reply to Jari-Matti Mäkelä,
> I think the first class tuples -bug covers this too:
>
> http://d.puremagic.com/issues/show_bug.cgi?id=1293
>
> If you feel it's worth a new bug report, please do as you wish.
I just added my comments to that bug
|
July 17, 2007 Re: Why can't templates use tuples for for argument types? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Don Clugston | Reply to Don,
> int [] is not an allowed as a template value parameter. Only char[],
> wchar[], dchar[], integers, and floating-point types can be template
> value parameters. (In C++, only integers are allowed).
>
strange, this works
|template Types(A...)
|{
| template Values(B...)
| {
| }
|}
|
|alias Types!(int, bool, int[]).Values!(1,true,[1,2,3]) bob;
And while I'm thinking about it; why isn't there something like a "one space tuple"?
A few times, I have wanted a template that works just like a tuple taking template but where one or more of the spots must be filled and a keep separate from the rest.
template Foo(A, B...) {}
these should work
Foo!(1, int);
Foo!(int, 1);
Foo!(Foo!(int, 1));
this shouldn't
Foo!();
(alias dosen't work)
|
July 17, 2007 Re: Why can't templates use tuples for for argument types? | ||||
---|---|---|---|---|
| ||||
Posted in reply to BCS | "BCS" <ao@pathlink.com> wrote in message news:ce0a3343bf108c99670e3f3ff2c@news.digitalmars.com... > > A few times, I have wanted a template that works just like a tuple taking template but where one or more of the spots must be filled and a keep separate from the rest. > > template Foo(A, B...) {} > > these should work > > Foo!(1, int); > Foo!(int, 1); > Foo!(Foo!(int, 1)); > > this shouldn't > > Foo!(); template Foo(A...) { static assert(A.length >= 1, "Foo needs at least one parameter"); do something with A[0]; do something else with A[1 .. $]; } |
July 17, 2007 Re: Why can't templates use tuples for for argument types? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jarrett Billingsley | Reply to Jarrett,
> "BCS" <ao@pathlink.com> wrote in message
> news:ce0a3343bf108c99670e3f3ff2c@news.digitalmars.com...
>
>> A few times, I have wanted a template that works just like a tuple
>> taking template but where one or more of the spots must be filled and
>> a keep separate from the rest.
>>
>> template Foo(A, B...) {}
>>
>> these should work
>>
>> Foo!(1, int);
>> Foo!(int, 1);
>> Foo!(Foo!(int, 1));
>> this shouldn't
>>
>> Foo!();
>>
> template Foo(A...)
> {
> static assert(A.length >= 1, "Foo needs at least one parameter");
> do something with A[0];
> do something else with A[1 .. $];
> }
How did you hack my system!!!! I'm sure you copied that right off my hard drive }:-|
<g>
All joking aside, I keep running into that so often that I want a cleaner way to do it. I want the proper usage documented in the code, not the comments and the asserts. I want to be able to talk about things by name without having to make aliases. It's a minor point but...
|
July 17, 2007 Re: Why can't templates use tuples for for argument types? | ||||
---|---|---|---|---|
| ||||
Posted in reply to BCS | "BCS" <ao@pathlink.com> wrote in message news:ce0a3343bf268c99691d2e8b71a@news.digitalmars.com... > > How did you hack my system!!!! I'm sure you copied that right off my hard drive }:-| > > <g> > All joking aside, I keep running into that so often that I want a cleaner > way to do it. I want the proper usage documented in the code, not the > comments and the asserts. I want to be able to talk about things by name > without having to make aliases. It's a minor point but... > The issue is that there's currently no way to specify that a template parameter can be 'anything'. T means it's a type, alias T means it's a symbol, and <sometype> T means it's a value. If you could specify that a parameter could take anything, this would be trivial. How about using .. - it means it's kind of like a tuple, but shorter ;) template Foo(A.., B...) { } Or, take a page from Erlang: template Foo(A | B...) { } In this case, A can be anything, and not just a type, because it's on the left of a bar. An issue with this, however, is that you can't have a type parameter. |
Copyright © 1999-2021 by the D Language Foundation