Inspecting __traits(isDeprecated) and deprecation warnings

Anonymouse zorael at gmail.com
Tue Sep 24 17:01:46 UTC 2019


I want to write a piece of code that reflects on the names of 
members of a passed struct, where some are depreacted.

https://run.dlang.io/is/P9EtRG

struct Foo
{
     string s;
     int ii;
     bool bbb;

     deprecated("Use `s`")
     string zzzz;
}

template longestMemberLength(T)
{
     enum longestMemberLength = ()
     {
         size_t maxLength;

         foreach (immutable i, immutable name; 
__traits(allMembers, T))
         {
             static if (!__traits(isDeprecated, 
__traits(getMember, T, name)))
             {
                 maxLength = max(maxLength, name.length);
             }
         }

         return maxLength;
     }();
}

static assert (longestMemberLength!Foo == "bbb".length);

> onlineapp.d(23): Deprecation: variable `onlineapp.Foo.zzzz` is 
> deprecated - Use s

Is there any way to inspect the deprecated-ness of a member this 
way? I only have what __traits(allMembers) gives me.


More information about the Digitalmars-d-learn mailing list