www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Looking for a workaround

reply Guillaume Piolat <first.last gmail.com> writes:
This program fails to build:


     import std.traits: getSymbolsByUDA;

     struct MyUDA
     {
     }

     class A
     {
          MyUDA int a;
     }

     class B : A
     {
          MyUDA int b;
     }

     void main()
     {
         alias G = getSymbolsByUDA!(B, MyUDA);
     }



Output:


     
c:\d\ldc2-1.28.0-windows-multilib\bin\..\import\std\traits.d(8933): Error:
template
     instance `AliasSeq!(b, a)` `AliasSeq!(b, a)` is nested in 
both `B` and `A`
     
c:\d\ldc2-1.28.0-windows-multilib\bin\..\import\std\traits.d(8707): Error:
template
     instance `std.traits.getSymbolsByUDAImpl!(B, MyUDA, "b", "a", 
"toString", "toHash",
     "opCmp", "opEquals", "Monitor", "factory")` error 
instantiating
     main.d(19):        instantiated from here: 
`getSymbolsByUDA!(B, MyUDA)`
     Failed: 
["c:\\d\\ldc2-1.28.0-windows-multilib\\bin\\ldmd2.exe", "-v", 
"-o-", "main.d", "-I."]


Any idea how to workaround that? I really need the same UDA in 
parent and child class.
Apr 06 2022
next sibling parent reply Adam D Ruppe <destructionator gmail.com> writes:
On Wednesday, 6 April 2022 at 18:10:32 UTC, Guillaume Piolat 
wrote:
 Any idea how to workaround that?
Works fine if you just use the language instead of the buggy phobos wrappers: --- struct MyUDA { } class A { MyUDA int a; } class B : A { MyUDA int b; } void main() { foreach(memberName; __traits(allMembers, B)) foreach(attr; __traits(getAttributes, __traits(getMember, B, memberName))) static if(is(attr == MyUDA)) pragma(msg, memberName); // a, b } --- So make a function that does that and applies whatever it is you need to apply and you're in business. Note that it is `is(typeof(attr) == MyUDA)` if defined ` MyUDA(args)`.
Apr 06 2022
parent Guillaume Piolat <first.last gmail.com> writes:
On Wednesday, 6 April 2022 at 18:21:11 UTC, Adam D Ruppe wrote:
 On Wednesday, 6 April 2022 at 18:10:32 UTC, Guillaume Piolat 
 wrote:
 Any idea how to workaround that?
Works fine if you just use the language instead of the buggy phobos wrappers: --- struct MyUDA { } class A { MyUDA int a; } class B : A { MyUDA int b; } void main() { foreach(memberName; __traits(allMembers, B)) foreach(attr; __traits(getAttributes, __traits(getMember, B, memberName))) static if(is(attr == MyUDA)) pragma(msg, memberName); // a, b } --- So make a function that does that and applies whatever it is you need to apply and you're in business. Note that it is `is(typeof(attr) == MyUDA)` if defined ` MyUDA(args)`.
Thanks, it will also create less templates.
Apr 06 2022
prev sibling parent reply MoonlightSentinel <moonlightsentinel disroot.org> writes:
On Wednesday, 6 April 2022 at 18:10:32 UTC, Guillaume Piolat 
wrote:
 Any idea how to workaround that? I really need the same UDA in 
 parent and child class.
Use a frontend >= dmd 2.099, it works according to run.dlang.io.
Apr 07 2022
parent Guillaume Piolat <first.last google.com> writes:
On Thursday, 7 April 2022 at 12:56:05 UTC, MoonlightSentinel 
wrote:
 On Wednesday, 6 April 2022 at 18:10:32 UTC, Guillaume Piolat 
 wrote:
 Any idea how to workaround that? I really need the same UDA in 
 parent and child class.
Use a frontend >= dmd 2.099, it works according to run.dlang.io.
Good to know, thanks.
Apr 07 2022