www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - __traits(parent) borken on templates

reply Shachar Shemesh <shachar weka.io> writes:
module foo;

import std.traits;

void bar(T)() {
}

pragma(msg, "id:          ", fullyQualifiedName!(bar));
pragma(msg, "parent:      ", fullyQualifiedName!(__traits(parent, bar)));

pragma(msg, "id:          ", fullyQualifiedName!(bar!int));
pragma(msg, "parent:      ", fullyQualifiedName!(__traits(parent, 
bar!int)));
pragma(msg, "grandparent: ", fullyQualifiedName!(__traits(parent, 
__traits(parent, bar!int))));


Compile, and the result is:
id:          foo.bar
parent:      foo
id:          foo.bar!(int)
parent:      foo.bar!(int)
grandparent: foo.bar!(int)

Once the object you have is an inside template, you cannot get its 
parent. I suspect the reason is that bar is actually defined as:

template bar(T) {
   void bar() {
   }
}

So asking for the function's parent supposedly returns the template. 
This doesn't live up to scrutiny, though, because the template's parent 
is the module, but asking for the parent of the parent still returns the 
function.
Dec 12 2017
parent reply Stefan Koch <uplink.coder googlemail.com> writes:
On Tuesday, 12 December 2017 at 14:49:04 UTC, Shachar Shemesh 
wrote:
 module foo;

 import std.traits;

 [...]
A template is not a symbol as is. And eponymous templates alias them-selfs to the symbol they define on instancing
Dec 12 2017
parent Shachar Shemesh <shachar weka.io> writes:
On 12/12/17 17:20, Stefan Koch wrote:
 On Tuesday, 12 December 2017 at 14:49:04 UTC, Shachar Shemesh wrote:
 module foo;

 import std.traits;

 [...]
A template is not a symbol as is. And eponymous templates alias them-selfs to the symbol they define on instancing
And yet, "bar", which is a template, works, but "bar!int", which is an identifier, doesn't. Shachar
Dec 12 2017