So, User-Defined Attributes

Max Samukha maxsamukha at gmail.com
Fri Jan 4 08:49:03 PST 2013


On Friday, 4 January 2013 at 16:04:59 UTC, Philippe Sigaud wrote:
> So, I'm testing how to transfer UDA from one symbol to another.
>
> ```
> auto transferAttributes(alias origin, To)(To t)
> {
>     @(__traits(getAttributes, origin)) To temp = t;
>     pragma(msg, __traits(getAttributes, temp));
>     return temp;
> }
>
> void main()
> {
>     @(3, "hello") int i = 10;
>     writeln("[",__traits(getAttributes, i), "]");
>
>     double d = 3.14;
>     auto d2 = transferAttributes!(i)(d);

transferAttributes attaches the attributes to the local 
declaration. I'd be surprised if they were copied to the outer 
declaration. Or did you mean attributing the return *type*?

>     // Doesn't work
>     writeln("[",__traits(getAttributes, d2), "]");
>
>     // Works:
>     @(__traits(getAttributes, i)) double d3 = 3.14;
>     writeln("[",__traits(getAttributes, d3), "]");
> }
> ```
>
> Begins at the end: d3 (a double) indeed ends up as having i (an 
> int) attributes. All is well and good.
>
> But transferAttributes does not work: inside its scope, temp 
> has the attributes from origin, but not when it gets out.
>
> Also, dropping the auto return is not accepted by the compiler, 
> even though it seems natural for me:
>
> @(__traits(getAttributes, origin)) To transferAttributes(alias 
> origin, To)(To t)
> {
>     @(__traits(getAttributes, origin)) To temp = t;
> ...
>
> 'origin' is a template parameter and should be reachable to 
> determine the return type.

That's a bug. Anyway, you are attributing the function 
declaration here, not the return type, which is another problem 
as it looks we do not have a syntax for that and "auto" is 
required:

auto foo()
{
     @attr struct S {}
     return S.init;
}







More information about the Digitalmars-d mailing list