Proposal: user defined attributes

Tove tove at fransson.se
Wed Mar 21 01:29:22 PDT 2012


On Wednesday, 21 March 2012 at 08:08:12 UTC, Jacob Carlborg wrote:
> On 2012-03-21 01:35, Adam D. Ruppe wrote:
>> On Wednesday, 21 March 2012 at 00:03:28 UTC, James Miller 
>> wrote:
>> So you'd just very simply do:
>>
>> struct MyAttribute { bool activated; }
>>
>> // @note is just like doing internalTuple ~= MyAttribute(true)
>> // and MyAttribute is of course just a plain old struct
>> initializer
>> @note(MyAttribute(true)) int a;
>>
>> To check it:
>>
>> foreach(note; __traits(getNotes, member_a))
>> static if(is(typeof(note) == MyAttribute) {
>> // do what you want here, ignore types you don't know
>> }
>
> That's basically my initial proposal and how annotations work 
> in Java.
>
>>> What data can be packed into annotations?
>>
>> Being able to use custom types is an
>> important part of my idea here.
>
> I think any type that a template can take (as a value) + other 
> attributes/annotations.

With the mixin improvement proposal any arbitrarily complex 
feature can be implemented in the library, appearing to enjoy 
first class syntax with just 1 extra character penalty vs the 
compiler.

#struct Foo
{
   @NonSerialized int x;
   @NonSerialized int y;
   int z;
}

Just imagine the next step, if the CTFE interface was based on 
AST:s instead of strings...



More information about the Digitalmars-d mailing list