Thread overview | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
December 18, 2014 Why do std.traits use template foo(args...) instead of foo(alias arg) ? | ||||
---|---|---|---|---|
| ||||
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 ? |
December 18, 2014 Re: Why do std.traits use template foo(args...) instead of foo(alias arg) ? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Mathias LANG | 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 |
December 18, 2014 Re: Why do std.traits use template foo(args...) instead of foo(alias arg) ? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Mathias LANG | 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.
|
December 18, 2014 Re: Why do std.traits use template foo(args...) instead of foo(alias arg) ? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Low Functioning Attachments: | 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. template t0(T...) if (T.length == 1) { enum t0 = T[0].stringof; } template t1(alias T) { enum t1 = 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 declaration 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 |
December 18, 2014 Re: Why do std.traits use template foo(args...) instead of foo(alias arg) ? | ||||
---|---|---|---|---|
| ||||
Posted in reply to ketmar | 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
|
December 18, 2014 Re: Why do std.traits use template foo(args...) instead of foo(alias arg) ? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Steven Schveighoffer Attachments: | 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/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. yes, thank you. bad wording on my side. |
December 18, 2014 Re: Why do std.traits use template foo(args...) instead of foo(alias arg) ? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Dicebot | 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 ?
|
December 18, 2014 Re: Why do std.traits use template foo(args...) instead of foo(alias arg) ? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Mathias LANG | 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.
|
Copyright © 1999-2021 by the D Language Foundation