<div class="gmail_quote">On 6 April 2012 12:41, Johannes Pfau <span dir="ltr"><<a href="mailto:nospam@example.com">nospam@example.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
Am Fri, 06 Apr 2012 00:48:34 -0700<br>
schrieb Walter Bright <<a href="mailto:newshound2@digitalmars.com">newshound2@digitalmars.com</a>>:<br>
<div><div class="h5"><br>
> On 4/6/2012 12:35 AM, Alex Rønne Petersen wrote:<br>
> > It actually can be a problem. In .NET land, there are many<br>
> > attributes across many projects (and even in the framework itself)<br>
> > with the same names. It turns out that regular namespace lookup<br>
> > rules alleviate this problem.<br>
><br>
><br>
> Perhaps a better scheme is:<br>
><br>
>     enum foo = 3;<br>
><br>
>     ...<br>
><br>
>     @attr(foo) int x;<br>
><br>
> That way, foo will follow all the usual rules.<br>
><br>
<br>
</div></div>The last time custom attributes where discussed, a C# like model was<br>
proposed. Is there a good reason why we should deviate from the C#<br>
implementation?<br>
<br>
C#:<br>
<a href="http://msdn.microsoft.com/en-US/library/48zeb25s(v=vs.80).aspx" target="_blank">http://msdn.microsoft.com/en-US/library/48zeb25s(v=vs.80).aspx</a><br>
<a href="http://msdn.microsoft.com/en-US/library/sw480ze8(v=vs.100).aspx" target="_blank">http://msdn.microsoft.com/en-US/library/sw480ze8(v=vs.100).aspx</a><br>
<a href="http://msdn.microsoft.com/en-US/library/z919e8tw(v=vs.80).aspx" target="_blank">http://msdn.microsoft.com/en-US/library/z919e8tw(v=vs.80).aspx</a><br>
<br>
(C# attributes can be applied to almost everything, therefore sometimes<br>
 "disambiguating targets" is necessary. Probably not needed in D):<br>
<a href="http://msdn.microsoft.com/en-US/library/b3787ac0(v=vs.80).aspx" target="_blank">http://msdn.microsoft.com/en-US/library/b3787ac0(v=vs.80).aspx</a><br>
<br>
Syntax in D would be different of course, but I see absolutely no need<br>
for the redundant (and ugly) @attr.<br>
<br>
Declaring a custom attribute:<br>
---------<br>
module std.something;<br>
<br>
struct Author<br>
{<br>
    string name;<br>
    public this(string name)<br>
    {<br>
        <a href="http://this.name" target="_blank">this.name</a> = name;<br>
    }<br>
}<br>
---------<br>
<br>
Using it:<br>
---------<br>
import std.something; //Usual namespace lookup rules apply to attributes<br>
<br>
/*<br>
 * @Author(param) calls the constructor of the Author struct and<br>
 * attaches the struct instance to test. Probably @Author (without<br>
 * parenthesis) coud be made to mean std.something.Author.init<br>
 */<br>
@Author("Johannes Pfau") int test;<br>
---------<br>
<br>
Attaching attributes multiple times as in C# should be possible.<br>
<br>
Using reflection to get that attribute:<br>
---------<br>
if(__traits(hasAttribute, test, std.something.Author))<br>
{<br>
    Author[] authors = __traits(getAttribute, test,<br>
        std.something.Author);<br>
}<br>
---------<br>
<br>
An array is used here to support attaching the same attribute multiple<br>
times. Of course "auto authors = ..." should be usable here too.<br>
</blockquote></div><br><div>+1</div>