www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Variadic parameter of length 1

reply "Dominikus Dittes Scherkl" writes:
I have several times seen a construct

template foo(T...) if(T.length == 1)
{
     ...
}

What is that good for?
Why using variadic parameter if anyway exactly one parameter is
required?!?
Aug 20 2014
next sibling parent reply "monarch_dodra" <monarchdodra gmail.com> writes:
On Wednesday, 20 August 2014 at 15:11:53 UTC, Dominikus Dittes 
Scherkl wrote:
 I have several times seen a construct

 template foo(T...) if(T.length == 1)
 {
     ...
 }

 What is that good for?
 Why using variadic parameter if anyway exactly one parameter is
 required?!?
AFAIK, it's a historical workaround to accept T as either alias or not alias, as varargs have "auto alias". EG: foo!int //OK foo!"hello" //OK too
Aug 20 2014
parent reply "Dominikus Dittes Scherkl" writes:
On Wednesday, 20 August 2014 at 15:26:14 UTC, monarch_dodra wrote:
 AFAIK, it's a historical workaround to accept T as either alias 
 or not alias, as varargs have "auto alias". EG:

 foo!int //OK
 foo!"hello" //OK too
Ah, ok. And why historical? Is that not necessary anymore? What better solution is there today?
Aug 20 2014
next sibling parent reply Philippe Sigaud via Digitalmars-d-learn writes:
On Wed, Aug 20, 2014 at 5:34 PM, Dominikus Dittes Scherkl via
Digitalmars-d-learn <digitalmars-d-learn puremagic.com> wrote:
 On Wednesday, 20 August 2014 at 15:26:14 UTC, monarch_dodra wrote:
 AFAIK, it's a historical workaround to accept T as either alias or not
 alias, as varargs have "auto alias". EG:

 foo!int //OK
 foo!"hello" //OK too
Ah, ok. And why historical? Is that not necessary anymore? What better solution is there today?
No better solution that I know of. alias template parameters (alias a) match symbols (names, user-defined types) whereas type parameter (T) match only pure types. So when we need to match anything, we use (T...) if (T.length == 1)
Aug 20 2014
parent "Dominikus Dittes Scherkl" writes:
On Wednesday, 20 August 2014 at 15:37:18 UTC, Philippe Sigaud via 
Digitalmars-d-learn wrote:
 No better solution that I know of.

 alias template parameters (alias a) match symbols (names, 
 user-defined
 types) whereas type parameter (T) match only pure types.

 So when we need to match anything, we use (T...) if (T.length 
 == 1)
Ok, now it's clear. Thank you very much. Sometimes D is really a little weird...
Aug 20 2014
prev sibling parent reply "Dicebot" <public dicebot.lv> writes:
On Wednesday, 20 August 2014 at 15:34:30 UTC, Dominikus Dittes 
Scherkl wrote:
 And why historical? Is that not necessary anymore? What better 
 solution is there today?
Historical in a sense that distinct "can be anything" template parameter is probably a better approach but it is too late to change such core language part.
Aug 20 2014
parent reply "monarch_dodra" <monarchdodra gmail.com> writes:
On Wednesday, 20 August 2014 at 17:02:59 UTC, Dicebot wrote:
 On Wednesday, 20 August 2014 at 15:34:30 UTC, Dominikus Dittes 
 Scherkl wrote:
 And why historical? Is that not necessary anymore? What better 
 solution is there today?
Historical in a sense that distinct "can be anything" template parameter is probably a better approach but it is too late to change such core language part.
Yeah, what he said. It's a language artifact.
Aug 20 2014
parent ketmar via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> writes:
On Wed, 20 Aug 2014 17:47:36 +0000
monarch_dodra via Digitalmars-d-learn
<digitalmars-d-learn puremagic.com> wrote:

 Yeah, what he said. It's a language artifact.
by the way, it would be nice to have wiki page with such artifacts and their explanations.
Aug 20 2014
prev sibling parent "anonymous" <anonymous example.com> writes:
On Wednesday, 20 August 2014 at 15:11:53 UTC, Dominikus Dittes
Scherkl wrote:
 I have several times seen a construct

 template foo(T...) if(T.length == 1)
 {
     ...
 }

 What is that good for?
 Why using variadic parameter if anyway exactly one parameter is
 required?!?
That's because template alias parameters cannot take builtin types like int, and type parameters can only take types. But tuple elements can be anything. struct SomeType {} template a(X) {enum a = 0;} /* type parameter */ enum a1 = a!int; /* ok */ enum a2 = a!SomeType; /* ok */ version(none) enum a3 = a!42; /* Error: template instance a!42 does not match template declaration a(X) */ template b(alias x) {enum b = 0;} /* alias parameter */ version(none) enum b1 = b!int; /* Error: template instance b!int does not match template declaration b(alias x) */ enum b2 = b!SomeType; /* ok */ enum b3 = b!42; /* ok */ template c(x ...) if(x.length == 1) {enum c = 0;} /* tuple parameter */ enum c1 = c!int; /* ok */ enum c2 = c!SomeType; /* ok */ enum c3 = c!42; /* ok */
Aug 20 2014