need this for name of type string

Alex sascha.orlov at gmail.com
Tue Sep 10 11:16:57 UTC 2019


On Tuesday, 10 September 2019 at 10:32:29 UTC, Andre Pany wrote:
> Hi,
>
> following coding is throwing compiler error:
>   need this for name of type string
>
> The error disappears if I delete method0.
> My gut feeling is, this is a compiler bug?
>
> ---
> class C
> {
>     static this()
>     {
>         getT!(typeof(this))();
>     }
>
>     @Foo void method0(){}
>
>     @Foo("abc") void method1(){}
> }
>
> struct Foo
> {
>     string name;
> }
>
> void getT(T)()
> {
>     import std.traits: hasUDA, getUDAs;
>
>     static foreach(fieldName; __traits(allMembers, T))
>     {
>         static if (hasUDA!(__traits(getMember, T, fieldName), 
> Foo))
>         {
>             pragma(msg, getUDAs!(__traits(getMember, T, 
> fieldName), Foo)[0].name);
>         }
>     }
> }
>
> void main(){}
> ---
>
> Kind regards
> André

Don't think so. In case of @Foo, you don't instantiate an object. 
Therefore, name cannot exist. So... in this case, the UDA is a 
type, not an object you can query for a name.
It's more the like the example with SimpleAttr on the help page 
[1], I think.

[1] https://dlang.org/library/std/traits/get_ud_as.html


More information about the Digitalmars-d-learn mailing list