Is it possible to use an UDA to generate a struct inside a class ?
Basile Burg via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Tue Dec 30 09:42:37 PST 2014
I have a struct used to describe a property[1].
Its standard usage is represented in this simple example:
|class Bar {
| private uint fField0;
| private izPropDescriptor!uint descrField0;
| this() {
| descrField0.define(&field0, &field0, "field0");
| }
| public void field0(uint aValue) {fField0 = aValue;}
| public uint field0(){return fField0;}
|}
The descriptor is used by a property binding system or a
serializer.
The problem is that declaring a property descriptor is **very**
repetitive.
I've always wanted to find a way to generate a descriptor
automatically,
and finally today, while reading some random things on GH, I've
"found" that
the annotation system used in HibernateD[2] could be used. So far
I didnt get the point of UDA and never used them.
So I've created a basic UDA but, and then ? Can a descriptor be
created using my "attribute" ? How ?
|struct Setter {
| const char[] propertyName;
|}
|struct Getter {
| const char[] propertyName;
|}
|class Foo {
| private uint fField0;
| public @Setter("field0") void field0(uint aValue) {
| fField0 = aValue;
| }
| public @Getter("field0") uint field0(){
| return fField0;
| }
|}
------------------------
[1]:https://github.com/BBasile/Iz/blob/master/import/iz/properties.d#L27
[2]:https://github.com/buggins/hibernated/blob/master/source/hibernated/annotations.d#L15
More information about the Digitalmars-d-learn
mailing list