Variadic parameter of length 1

2014-08-20 Thread Dominikus Dittes Scherkl via Digitalmars-d-learn

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?!?


Re: Variadic parameter of length 1

2014-08-20 Thread monarch_dodra via Digitalmars-d-learn
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


Re: Variadic parameter of length 1

2014-08-20 Thread Dominikus Dittes Scherkl via Digitalmars-d-learn

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?


Re: Variadic parameter of length 1

2014-08-20 Thread anonymous via Digitalmars-d-learn

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 */


Re: Variadic parameter of length 1

2014-08-20 Thread Philippe Sigaud via Digitalmars-d-learn
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)


Re: Variadic parameter of length 1

2014-08-20 Thread Dominikus Dittes Scherkl via Digitalmars-d-learn
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...


Re: Variadic parameter of length 1

2014-08-20 Thread Dicebot via Digitalmars-d-learn
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.


Re: Variadic parameter of length 1

2014-08-20 Thread monarch_dodra via Digitalmars-d-learn

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.


Re: Variadic parameter of length 1

2014-08-20 Thread ketmar via Digitalmars-d-learn
On Wed, 20 Aug 2014 17:47:36 +
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.


signature.asc
Description: PGP signature