What have you done with UDAs?

Basile Burg via Digitalmars-d digitalmars-d at puremagic.com
Mon Jun 22 14:19:17 PDT 2015


On Monday, 22 June 2015 at 19:09:40 UTC, weaselcat wrote:
> I never seem to use them for anything, has anyone else done 
> anything interesting with them?

I use them to annotate the setter/getter pairs in classes. Two 
analyzers look for them to automatically create what i call a 
"property descriptor".

https://github.com/BBasile/iz/blob/master/import/iz/properties.d#L362

Property descriptors are then used to
- bind/synchronize several properties together.
- serialize or deserialize classes.

This is directly inspired by Pascal's "published" property. All 
the type information system is replaced with UDA and static 
analysis:

Pascal:

---
published property myProp: integer read getMyProp write setMyProp;
---

D2 equivalent:

---
@SetGet private int _field; // solution 1: use directly the field

@Set void myProp(int value){} // solution 2: use explicit 
accessors.
@Get int myProp(){return _field;}
---

The Annotations work very well with virtual methods. For example 
the most derivated (overriden) anotated @Set/@Get is always took 
by the analyzer, like expected.


More information about the Digitalmars-d mailing list