So, User-Defined Attributes
Philippe Sigaud
philippe.sigaud at gmail.com
Fri Jan 4 08:04:59 PST 2013
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);
// 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.
More information about the Digitalmars-d
mailing list