<div class="gmail_quote">On 6 April 2012 15:13, deadalnix <span dir="ltr"><<a href="mailto:deadalnix@gmail.com" target="_blank">deadalnix@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">

Le 06/04/2012 11:41, Johannes Pfau a écrit :<div><div><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" target="_blank">newshound2@digitalmars.<u></u>com</a>>:<br>
<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
On 4/6/2012 12:35 AM, Alex Rønne Petersen wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
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>
</blockquote>
<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>
</blockquote>
<br>
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-<u></u>US/library/48zeb25s(v=vs.80).<u></u>aspx</a><br>
<a href="http://msdn.microsoft.com/en-US/library/sw480ze8(v=vs.100).aspx" target="_blank">http://msdn.microsoft.com/en-<u></u>US/library/sw480ze8(v=vs.100).<u></u>aspx</a><br>
<a href="http://msdn.microsoft.com/en-US/library/z919e8tw(v=vs.80).aspx" target="_blank">http://msdn.microsoft.com/en-<u></u>US/library/z919e8tw(v=vs.80).<u></u>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-<u></u>US/library/b3787ac0(v=vs.80).<u></u>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>
<br>
</blockquote>
<br></div></div>
That is a really nice proposal !<br>
</blockquote></div><br><div>This is precisely how I have always imagined the system working. Walter: can you comment why you think this is not do-able, or not a good way to go about it?</div>