Custom attributes (again)

Jacob Carlborg doob at me.com
Fri Apr 6 05:32:49 PDT 2012


On 2012-04-06 08:47, Walter Bright wrote:
> On 4/5/2012 5:00 AM, Manu wrote:
>  > C# and Java both have attributes, following these established design
> patterns, I
>  > don't think there should be any mystery over how they should be
> implemented.
>
> I was thinking of something along the lines of what has been proposed
> here earlier:
>
> @attr(identifier = expression)
>
> as a storage class, like:
>
> @attr(foo = bar + 1) int x;
>
> and then:
>
> __traits(hasAttribute, x, foo)
>
> would return true, and:
>
> __traits(getAttribute, x, foo)
>
> would return the expression (bar+1). The expression would be
> compile-time only, evaluated at the point of declaration.
>
> The implementation is simple enough, just attach to each symbol an array
> of (identifier,expression) pairs.
>
> You could also omit the expression, and just have:
>
> @attr(bar) int y;

I'm not a particular fan of that syntax. I would go with:

@identifier(key = value/expression)

Like:

@foo(k1 = bar + 1, k2 = bar + 2) int x;

If the attribute takes one key-value pair:

@foo(value = bar + 1) int x;

Can be shortened like:

@foo(bar + 1) int x;

And for attributes that don't take any values:

@foo int x;

-- 
/Jacob Carlborg


More information about the Digitalmars-d mailing list