<div class="gmail_quote">On 9 March 2012 18:15, Andrei Alexandrescu <span dir="ltr"><<a href="mailto:SeeWebsiteForEmail@erdani.org">SeeWebsiteForEmail@erdani.org</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div class="im">On 3/9/12 2:56 AM, Manu wrote:<br>
</div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div class="im">
Does D have a nice way to add annotations or custom attributes to entities?<br>
<br></div><div class="im">
In Java/C# for example, it is common to annotate things with useful<br>
compile time information. I'd like to be able to do that in D on occasion.<br>
<br>
For instance, I'm serialising some struct/class using reflection to some<br>
text format, but there are a couple of members in a particular class<br>
that I don't want to be written.<br>
A nice solution might be that I could annotate various members:<br>
@DoNotSerialise int thing; ... or something along those lines, whatever<br>
concept you want to apply, which I could then inspect in static if()<br>
logic to produce some requested additional behaviour.<br>
<br>
This is a trivial example, but looking at C#/Java, you can see how many<br>
useful things can be done with this sort of system.<br>
How would it be done currently?<br>
</div></blockquote>
<br>
I think a good approach in D would be to define mixins that work in conjunction with the feature involved, for example:<br>
<br>
class A {<br>
    int thing;<br>
    mixin(DoNotSerialize!"thing");<br>
    ...<br>
}<br>
<br>
or together:<br>
<br>
class A {<br>
    mixin(DoNotSerialize!(int, "thing"));<br>
    ...<br>
}<span class="HOEnZb"><font color="#888888"><br>
<br>
<br>
Andrei<br>
</font></span></blockquote></div><br><div>Yep, that sure is horribly ugly! Sadly, that's what I thought the case was currently. Let's hope for user defined attributes in the future ;)</div>