partiallyQualifiedName?

Nicholas Wilson iamthewilsonator at hotmail.com
Tue Oct 17 07:58:23 UTC 2017


On Tuesday, 17 October 2017 at 06:38:52 UTC, Biotronic wrote:
> If I understand things correctly, you only care about enums 
> nested in scopes up to the module scope, right? If so, this 
> seems to fit the bill:
>
> enum A {a}
>
> struct S {
>     enum B {b}
>     struct S2 {
>         enum C {c}
>         C c;
>     }
>     A a;
>     B b;
>     int n, m;
>     pragma(msg, partiallyQualifiedName!n); // S.n
>     pragma(msg, partiallyQualifiedName!(S2)); // S.S2
>     pragma(msg, partiallyQualifiedName!(typeof(a))); // A
>     pragma(msg, partiallyQualifiedName!(typeof(b))); // S.B
>     pragma(msg, partiallyQualifiedName!(typeof(S2.c))); // 
> S.S2.C
>     pragma(msg, partiallyQualifiedName!(a)); // S.a
>     pragma(msg, partiallyQualifiedName!(b)); // S.b
>     pragma(msg, partiallyQualifiedName!(S2.c)); // S.S2.c
> }
>
> template isModule(alias a) {
>     static if (is(a) || is(typeof(a)) || a.stringof.length < 7) 
> {
>         enum isModule = false;
>     } else {
>         enum isModule = a.stringof[0..7] == "module ";
>     }
> }
>
> template partiallyQualifiedName(alias a) {
>     static if (isModule!a) {
>         enum partiallyQualifiedName = "";
>     } else {
>         static if (!isModule!(__traits(parent, a))) {
>             enum prefix = 
> partiallyQualifiedName!(__traits(parent, a)) ~ ".";
>         } else {
>             enum prefix = "";
>         }
>         enum partiallyQualifiedName = prefix ~ 
> __traits(identifier, a);
>     }
> }
>
> Note that it fails for built-in types, arrays, and many other 
> cases, and does not support const/immutable/shared/etc. It 
> should cover the cases described, though, and that's what's 
> most important. If more support is needed, consider it a 
> starting point, and feel free to ask for more. :)
>
> --
>   Biotronic

Yes I only need it for enums due to it being a new type, the rest 
are all builtin basic types. static if(is(typeof(X) == enum)) 
should do the trick for the rest.

Thanks!


More information about the Digitalmars-d-learn mailing list