User defined attributes use

Jacob Carlborg doob at me.com
Fri Sep 20 00:57:43 PDT 2013


On 2013-09-20 08:59, ilya-stromberg wrote:

> Can I explicitly specify when I can use attribute? Something like
> this:
>
> @attribute("field")
> struct matches(string mustMatch)
> {
> }
>
> string wrongAttribute
> {
> }
>
> class Foo
> {
>      @matches("[0-9]+")
>      string someNumber; //OK, it's a field
> }
>
> @matches("[0-9]+") //Error, it's a class, not a field
> class Bar
> {
> }
>
> @wrongAttribute //Error, this attribute doesn't exist
> class C
> {
> }

Unfortunately you can't. I tag all my structs which are supposed to be 
used as UDA's with a @attribute UDA.

struct attribute {}

@attribute struct matches(string mustMatch) {}

Then I have some wrappers around __traits(getAttributes) that will, by 
default, only return UDA's that them self have the @attribute UDA 
attached to them.

https://github.com/jacob-carlborg/orange/blob/master/orange/core/Attribute.d

-- 
/Jacob Carlborg


More information about the Digitalmars-d-learn mailing list