digitalmars.D.learn - Variadic parameter of length 1
- Dominikus Dittes Scherkl (8/8) Aug 20 2014 I have several times seen a construct
- monarch_dodra (6/14) Aug 20 2014 AFAIK, it's a historical workaround to accept T as either alias
- Dominikus Dittes Scherkl (4/8) Aug 20 2014 Ah, ok.
- Philippe Sigaud via Digitalmars-d-learn (6/16) Aug 20 2014 No better solution that I know of.
- Dominikus Dittes Scherkl (4/10) Aug 20 2014 Ok, now it's clear. Thank you very much.
- Dicebot (5/7) Aug 20 2014 Historical in a sense that distinct "can be anything" template
- monarch_dodra (2/9) Aug 20 2014 Yeah, what he said. It's a language artifact.
- ketmar via Digitalmars-d-learn (5/6) Aug 20 2014 On Wed, 20 Aug 2014 17:47:36 +0000
- anonymous (21/29) Aug 20 2014 That's because template alias parameters cannot take builtin
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
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
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 tooAh, ok. And why historical? Is that not necessary anymore? What better solution is there today?
Aug 20 2014
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: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)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 tooAh, ok. And why historical? Is that not necessary anymore? What better solution is there today?
Aug 20 2014
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
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
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:Yeah, what he said. It's a language artifact.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
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
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