So, User-Defined Attributes

Jacob Carlborg doob at me.com
Sun Jan 6 06:04:32 PST 2013


On 2013-01-06 14:29, Philippe Sigaud wrote:
>
>     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.

You can perhaps encapsulate this in a mixin?

-- 
/Jacob Carlborg


More information about the Digitalmars-d mailing list