<div class="gmail_quote">On 6 April 2012 09:47, Walter Bright <span dir="ltr"><<a href="mailto:newshound2@digitalmars.com">newshound2@digitalmars.com</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 4/5/2012 5:00 AM, Manu wrote:<br>
> C# and Java both have attributes, following these established design patterns, I<br>
> don't think there should be any mystery over how they should be implemented.<br>
<br></div>
I was thinking of something along the lines of what has been proposed here earlier:<br>
<br>
@attr(identifier = expression)<br>
<br>
as a storage class, like:<br>
<br>
@attr(foo = bar + 1) int x;<br>
<br>
and then:<br>
<br>
__traits(hasAttribute, x, foo)<br>
<br>
would return true, and:<br>
<br>
__traits(getAttribute, x, foo)<br>
<br>
would return the expression (bar+1). The expression would be compile-time only, evaluated at the point of declaration.<br>
<br>
The implementation is simple enough, just attach to each symbol an array of (identifier,expression) pairs.<br>
<br>
You could also omit the expression, and just have:<br>
<br>
@attr(bar) int y;<br>
</blockquote></div><br><div>I think this is unnecessarily verbose, although certainly workable as a minimum. Although there is one issue that 'foo' can't store compound data.</div><div>I'd like to see attributes defined as a special sort of struct, and support constructors, this way you can associate rich data, and you can perform complex expressions (ctfe) within the constructor to set defaults, or calculate other details about the attribute if not specified.</div>
<div><br></div><div>@replay int x; // this is all that is needed if the attribute associates no variable information</div><div><br></div><div>// or something associating consideraly more rich information</div><div>@editor("X coordinate", EditType.Slider, Colour.Red, "Verbose description about the thing") // this is still a lot easier to read to me...</div>
<div>int x;</div>