www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Cannot access template name from within template

reply Andrei Alexandrescu <SeeWebsiteForEmail erdani.org> writes:
Consider:

template SomethingCool(alias X) { alias Y = X!int; }

struct MyStruct(T) {
     alias A = SomethingCool!MyStruct;
}

Inside MyStruct though, a mention of the symbol MyStruct alone is 
actually the current instantiation - i.e. a type, not a template.

Any known workaround?


Thanks,

Andrei
Oct 03 2016
next sibling parent reply Stefan Koch <uplink.coder googlemail.com> writes:
On Monday, 3 October 2016 at 22:28:46 UTC, Andrei Alexandrescu 
wrote:
 Consider:

 template SomethingCool(alias X) { alias Y = X!int; }

 struct MyStruct(T) {
     alias A = SomethingCool!MyStruct;
 }

 Inside MyStruct though, a mention of the symbol MyStruct alone 
 is actually the current instantiation - i.e. a type, not a 
 template.

 Any known workaround?


 Thanks,

 Andrei
try defining an alias to template name outside of the template and use that. No guarantees.
Oct 03 2016
parent reply Andrei Alexandrescu <SeeWebsiteForEmail erdani.org> writes:
On 10/03/2016 06:32 PM, Stefan Koch wrote:
 On Monday, 3 October 2016 at 22:28:46 UTC, Andrei Alexandrescu wrote:
 Consider:

 template SomethingCool(alias X) { alias Y = X!int; }

 struct MyStruct(T) {
     alias A = SomethingCool!MyStruct;
 }

 Inside MyStruct though, a mention of the symbol MyStruct alone is
 actually the current instantiation - i.e. a type, not a template.

 Any known workaround?


 Thanks,

 Andrei
try defining an alias to template name outside of the template and use that. No guarantees.
Using std.traits.TemplateOf!MyStruct works like a charm. -- Andrei
Oct 03 2016
parent reply Michael Coulombe <kirsybuu gmail.com> writes:
On Tuesday, 4 October 2016 at 00:40:08 UTC, Andrei Alexandrescu 
wrote:
 On 10/03/2016 06:32 PM, Stefan Koch wrote:
 On Monday, 3 October 2016 at 22:28:46 UTC, Andrei Alexandrescu 
 wrote:
 Consider:

 template SomethingCool(alias X) { alias Y = X!int; }

 struct MyStruct(T) {
     alias A = SomethingCool!MyStruct;
 }

 Inside MyStruct though, a mention of the symbol MyStruct 
 alone is
 actually the current instantiation - i.e. a type, not a 
 template.

 Any known workaround?


 Thanks,

 Andrei
try defining an alias to template name outside of the template and use that. No guarantees.
Using std.traits.TemplateOf!MyStruct works like a charm. -- Andrei
You can also just do something like this, to search the upper/global scope: alias A = SomethingCool!(.MyStruct);
Oct 03 2016
parent Jacob Carlborg <doob me.com> writes:
On 2016-10-04 04:22, Michael Coulombe wrote:

 You can also just do something like this, to search the upper/global scope:

     alias A = SomethingCool!(.MyStruct);
Doesn't work for nested templates. -- /Jacob Carlborg
Oct 03 2016
prev sibling parent reply Andrej Mitrovic <andrej.mitrovich gmail.com> writes:
On Monday, 3 October 2016 at 22:28:46 UTC, Andrei Alexandrescu 
wrote:
 Any known workaround?
If you're ok with the hacky approach: ----- import std.algorithm; import std.conv; /// these are in std.traits but private for whatever reason alias Identity(alias A) = A; alias parentOf(alias sym) = Identity!(__traits(parent, sym)); alias parentOf(alias sym : T!Args, alias T, Args...) = Identity!(__traits(parent, T)); /// alias itself to the symbol of template instance T template TemplateOf(T) { enum name = T.stringof; // strip everything after '!' (instantiation args) enum unqual_name = name.until!(a => a == '!').to!string; mixin("alias TemplateOf = parentOf!T." ~ unqual_name ~ ";"); } template SomethingCool(alias X) { pragma(msg, X.stringof); // sanity check alias Y = X!int; } struct MyStruct(T) { alias A = SomethingCool!(TemplateOf!MyStruct); } struct Outer { struct Nested(T) { alias A = SomethingCool!(TemplateOf!Nested); } } void main ( ) { alias MyStruct!int X; alias Outer.Nested!int Y; } -----
Oct 04 2016
parent reply Andrei Alexandrescu <SeeWebsiteForEmail erdani.org> writes:
On 10/04/2016 06:03 PM, Andrej Mitrovic wrote:
 template TemplateOf(T)
Have you forgotten you submitted this to phobos? :o) -- Andrei
Oct 04 2016
parent Andrej Mitrovic <andrej.mitrovich gmail.com> writes:
On Tuesday, 4 October 2016 at 22:43:21 UTC, Andrei Alexandrescu 
wrote:
 On 10/04/2016 06:03 PM, Andrej Mitrovic wrote:
 template TemplateOf(T)
Have you forgotten you submitted this to phobos? :o) -- Andrei
I forgot about that template. But that wasn't me: https://github.com/dlang/phobos/pull/1884 :) I added isInstanceOf https://github.com/dlang/phobos/commit/e02593090a909948183921f6f00039ce3f37acff
Oct 04 2016