Inspecting __traits(isDeprecated) and deprecation warnings

Anonymouse zorael at gmail.com
Wed Sep 25 14:20:00 UTC 2019


On Wednesday, 25 September 2019 at 05:57:19 UTC, Tobias Pankrath 
wrote:
> Does your code work or does it not? I don't seem to unterstand 
> neither what the question here is nor what the desired result 
> is. Is the problem that the static reflections triggers the 
> deprecation warning?

I added some deprecations in my project and am going through my 
templates trying to silence the warnings that suddenly popped up. 
This template works, but it triggers deprecation warnings when I 
am actively trying to avoid them.

getMember in _traits(isDeprecated, __traits(getMember, T, name)) 
causes a warning on deprecated symbols, which I wanted to avoid 
with isDeprecated, but I couldn't without first calling getMember 
to get the symbol to evaluate it. There's no way to combine 
isDeprecated with getMember without getting a warning.

I worked around the issue by using .tupleof instead of getMember, 
which breaks it for classes (.tupleof needs a `this` and 
SomeClass.init can't be it) but silences warnings for structs.

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

import std;

void main()
{
     static assert(longestMemberName!Foo == "bbb".length);
}

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

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

template longestMemberName(T)
if (is(T == struct))
{
     enum longestMemberName = ()
     {
         size_t maxLength;
         T thing;  // need a `this`

         foreach (immutable i, member; thing.tupleof)
         {
             static if (!__traits(isDeprecated, thing.tupleof[i]) 
&&
                        !isType!(thing.tupleof[i]))
             {
                 enum name = __traits(identifier, 
thing.tupleof[i]);
                 maxLength = max(maxLength, name.length);
             }
         }

         return maxLength;
     }();
}


More information about the Digitalmars-d-learn mailing list