So, User-Defined Attributes

Philippe Sigaud philippe.sigaud at gmail.com
Sun Jan 6 05:29:13 PST 2013


> Don't know if this is what you want but:
>
> struct Marked {}
> struct Attrs (T...) {}
>
> auto bar ()
> {
>     @Marked int i;
>     return Attrs!(__traits(getAttributes, i))();
> }
>
> void main ()
> {
>     writeln(bar()); // prints Attrs!(Marked)()
> }


I just want to be able to return an attributed something. How can a
function return something that's attributed?

IIUC what Walter said, a function cannot return an attributed value: any
internal symbol can be attributed, but these cannot get out.
I can create can attributed value to 'catch' what a function returns, but
not automatically:

??? attributedInt()
{
    @("Hello") int i = 1;
    return i;
}

void main()
{
    ??? j = attributedInt();
    // How to have j get the @("Hello") attribute?
}

The only way would be what you suggest:

- extract the attributes from the internal i
- store them in a specially-crafted struct
- return that
- in the external code, catch the returned struct
- extract the artificially stored attributes
- generate a new value with the same attributes.


Ugh.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.puremagic.com/pipermail/digitalmars-d/attachments/20130106/3c77e8f8/attachment-0001.html>


More information about the Digitalmars-d mailing list