hasUDA with this

Simen Kjærås simen.kjaras at gmail.com
Fri Jul 20 07:25:23 UTC 2018


On Thursday, 19 July 2018 at 19:18:43 UTC, jmh530 wrote:
> However, it seems like hasUDA doesn't seem to produce the 
> result I would have expected here. I tried using getAttributes, 
> but that didn't work either.
>
> Am I missing something, or should I submit an enhancement 
> request?

UDAs apply to symbols. When you pass something to a function, the 
symbol does not go with the value, and thus that UDA goes away. 
It's similar to assigning to a different variable:

     import std.traits : hasUDA;
     @("foo")
     Foo foo1;
     Foo foo2 = foo1;
     assert(!hasUDA!(foo2, "foo"));

Since UDAs are compile-time constructs, they really can't follow 
values around in that way. Consider:

void main(string[] args) {
     @("foo")
     Foo foo1;
     @("bar")
     Foo foo2;
     Foo foo3 = (args.length % 2) ? foo1 : foo2;
     assert(hasUDA!(foo3, "foo")); // Would you expect this to 
pass or fail?
}

Timoses first checkUDA function is exactly what I would suggest 
you use.

--
   Simen


More information about the Digitalmars-d-learn mailing list