www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Using traits to detect alias declaration

reply "cal" <callumenator gmail.com> writes:
When using __traits(allMembers), I'd like to be able to detect if 
a given member is an alias (and get the member that is aliased). 
Is there a way to do this currently?
Sep 09 2012
next sibling parent Jonathan M Davis <jmdavisProg gmx.com> writes:
On Monday, September 10, 2012 05:01:05 cal wrote:
 __traits(allMembers), I'd like to be able to detect if 
 a given member is an alias (and get the member that is aliased). 
 Is there a way to do this currently?

No. As far as the compiler is concerned, there is no difference between an alias and what it's aliased from. Now, if you're talking about alias this in particular rather than a type or variable, that's a bit different, because it only gets used if the non-alias stuff doesn't work. If that's the case, you _might_ be able to determine whether something is using alias this by checking that == on the type fails and : succeeds (since aside from built-in types, the only times that implicit conversions exist are via inheritance or alias this), but that won't tell you _what_ was aliased to this. I don't think that there's any way to determine that. - Jonathan M Davis
Sep 09 2012
prev sibling parent "cal" <callumenator gmail.com> writes:
On Monday, 10 September 2012 at 03:30:25 UTC, Jonathan M Davis 
wrote:
 On Monday, September 10, 2012 05:01:05 cal wrote:
 __traits(allMembers), I'd like to be able to detect if a given 
 member is an alias (and get the member that is aliased). Is 
 there a way to do this currently?

No. As far as the compiler is concerned, there is no difference between an alias and what it's aliased from.

Played around a bit, and this _seems_ to work: // Detect alias foreach(member; __traits(allMembers, l.main)) { static if (__traits(compiles, __traits(identifier, mixin(member)))) { string ident = __traits(identifier, mixin(member)); if (ident != member) /// Detected an alias } } But I wonder if this is reliable.
Sep 09 2012