www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Why do std.traits use template foo(args...) instead of foo(alias arg)

reply "Mathias LANG" <geod24 gmail.com> writes:
An exemple being fullyQualifiedName:
https://github.com/D-Programming-Language/phobos/blob/master/std/traits.d#L415

I don't get this pattern. Is it documented somewhere ?
Dec 18 2014
next sibling parent reply "Low Functioning" <nathan.judge gmail.com> writes:
On Thursday, 18 December 2014 at 15:48:02 UTC, Mathias LANG wrote:
 An exemple being fullyQualifiedName:
 https://github.com/D-Programming-Language/phobos/blob/master/std/traits.d#L415

 I don't get this pattern. Is it documented somewhere ?
http://dlang.org/variadic-function-templates.html
Dec 18 2014
parent reply ketmar via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> writes:
On Thu, 18 Dec 2014 15:52:06 +0000
Low Functioning via Digitalmars-d-learn
<digitalmars-d-learn puremagic.com> wrote:

 On Thursday, 18 December 2014 at 15:48:02 UTC, Mathias LANG wrote:
 An exemple being fullyQualifiedName:
 https://github.com/D-Programming-Language/phobos/blob/master/std/traits=
.d#L415
 I don't get this pattern. Is it documented somewhere ?
=20 http://dlang.org/variadic-function-templates.html
that's not a question about "what it does?", but the question about "why it did this way?" the answer is simple: `alias` arguments can't accept types. i.e. template t0(T...) if (T.length =3D=3D 1) { enum t0 =3D T[0].stringof; } template t1(alias T) { enum t1 =3D T.stringof; } pragma(msg, t0!int); pragma(msg, t1!int); # dmd -c -o- test.d int test.d(11): Error: template instance t1!int does not match template decla= ration t1(alias T) test.d(11): while evaluating pragma(msg, t1!int) but: int a; pragma(msg, t0!a); pragma(msg, t1!a); gives: a a i.e. (T...) can accept both types and symbols, and `alias` can accept only symbols. `fullyQualifiedName` then checks if T is symbol or type: static if (is(T)) ... // T is a type, process as type else ... // T is a symbold, process as symbol
Dec 18 2014
parent reply Steven Schveighoffer <schveiguy yahoo.com> writes:
On 12/18/14 11:21 AM, ketmar via Digitalmars-d-learn wrote:
 On Thu, 18 Dec 2014 15:52:06 +0000
 Low Functioning via Digitalmars-d-learn
 <digitalmars-d-learn puremagic.com> wrote:

 On Thursday, 18 December 2014 at 15:48:02 UTC, Mathias LANG wrote:
 An exemple being fullyQualifiedName:
 https://github.com/D-Programming-Language/phobos/blob/master/std/traits..d#L415

 I don't get this pattern. Is it documented somewhere ?
http://dlang.org/variadic-function-templates.html
that's not a question about "what it does?", but the question about "why it did this way?" the answer is simple: `alias` arguments can't accept types. i.e.
s/types/keywords alias can accept types as long as they are symbols. For instance: struct S {} pragma(msg, t1!S); // works. -Steve
Dec 18 2014
parent ketmar via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> writes:
On Thu, 18 Dec 2014 11:26:20 -0500
Steven Schveighoffer via Digitalmars-d-learn
<digitalmars-d-learn puremagic.com> wrote:

 On 12/18/14 11:21 AM, ketmar via Digitalmars-d-learn wrote:
 On Thu, 18 Dec 2014 15:52:06 +0000
 Low Functioning via Digitalmars-d-learn
 <digitalmars-d-learn puremagic.com> wrote:

 On Thursday, 18 December 2014 at 15:48:02 UTC, Mathias LANG wrote:
 An exemple being fullyQualifiedName:
 https://github.com/D-Programming-Language/phobos/blob/master/std/trai=
ts..d#L415
 I don't get this pattern. Is it documented somewhere ?
http://dlang.org/variadic-function-templates.html
that's not a question about "what it does?", but the question about "why it did this way?" the answer is simple: `alias` arguments can't accept types. i.e.
=20 s/types/keywords =20 alias can accept types as long as they are symbols. =20 For instance: =20 struct S {} =20 pragma(msg, t1!S); // works.
yes, thank you. bad wording on my side.
Dec 18 2014
prev sibling parent reply "Dicebot" <public dicebot.lv> writes:
On Thursday, 18 December 2014 at 15:48:02 UTC, Mathias LANG wrote:
 An exemple being fullyQualifiedName:
 https://github.com/D-Programming-Language/phobos/blob/master/std/traits.d#L415

 I don't get this pattern. Is it documented somewhere ?
Full pattern looks like this: template foo(T...) if (T.length == 1) This is a way to workaround D template argument limitation - you can't have any parameter that accepts both types and symbols (`alias T` and `T` at once) other than variadic parameter. Limit variadic length to 1 and you emulate such "accepts anything" parameter.
Dec 18 2014
parent reply "Mathias LANG" <geod24 gmail.com> writes:
On Thursday, 18 December 2014 at 16:10:31 UTC, Dicebot wrote:
 On Thursday, 18 December 2014 at 15:48:02 UTC, Mathias LANG 
 wrote:
 An exemple being fullyQualifiedName:
 https://github.com/D-Programming-Language/phobos/blob/master/std/traits.d#L415

 I don't get this pattern. Is it documented somewhere ?
Full pattern looks like this: template foo(T...) if (T.length == 1) This is a way to workaround D template argument limitation - you can't have any parameter that accepts both types and symbols (`alias T` and `T` at once) other than variadic parameter. Limit variadic length to 1 and you emulate such "accepts anything" parameter.
Thanks for the precision. Is it something that is going to be fixed, or is it by design ?
Dec 18 2014
parent "Dicebot" <public dicebot.lv> writes:
On Thursday, 18 December 2014 at 17:20:28 UTC, Mathias LANG wrote:
 Thanks for the precision. Is it something that is going to be 
 fixed, or is it by design ?
It is something that most likely would have been designed differently if we had another chance (aka D3) but unlikely to change in D2 as existing pattern works good enough in practice. It is confusing but usually only needed for power user libraries thus learning curve impact is low.
Dec 18 2014