Proposal: user defined attributes

Jacob Carlborg doob at me.com
Mon Mar 19 01:26:18 PDT 2012


On 2012-03-19 01:41, Walter Bright wrote:
> On 3/18/2012 2:58 PM, Jacob Carlborg wrote:
>> Ok, now to the user defined attribute:
>>
>> @attribute class NonSerialized {} // This is what's called a "marker"
>> in Java.
>> It doesn't contain any information, the information is the type itself.
>>
>> @attribute class SerializeAs
>> {
>> string value;
>> }
>>
>> class Base
>> {
>> int x;
>> @SerializeAs(foo) int y;
>> @NonSerialized int c;
>> }
>>
>> @NonSerialized class Sub : Base
>> {
>> int z;
>> }
>>
>> Base sub = new Sub;
>> serialize(sub);
>>
>> In this example the user defined attribute "NonSerialized" indicates
>> that the
>> class shouldn't be serialized. When the serializer is serializing
>> "sub" the
>> compile time type will be "Base" therefore it would need to use runtime
>> reflection to also serialize the fields added in the subclass "Sub".
>> To check if
>> the object should be serialized the serializer need to be able to
>> access user
>> defined attributes using runtime reflection.
>
> I'm sorry, I find this *massively* confusing. What is foo?

It should have been:

@SerializeAs("foo") int y;

"foo" is the name "y" will be serialized with. Just to show an example 
with an attribute accepting a value.

> Why are you
> serializing something marked "NonSerialized"? I really have no idea what
> is going on with this. What is the serialize() function? How does any of
> this tell you how to serialize an int? How is Base.c also a
> NonSerialized with c in a superclass of it?

This example might not have been the best one. I might not know that 
"Sub" is marked as "NonSerialized". An instance of "Sub" can be part of 
another class that is not marked as "NonSerialized".

"serialize" would be the function that performs the serialization. I 
cannot write a whole serialization in a newsgroup message, can I :)

This example also shows that "NonSerialized" can be applied to 
individual fields of a class.

This tell how to serialize an int. It tells if a part of an object 
hierarchy (class, or field) should be serialized or not.

> ??????????????????????????????????
>
> Maybe this is why I don't much care for attributes - it's all just a fog
> to me what is happening.

I think it's quite clear what is happening. But I've might squeezed in 
too much in a too small example.

-- 
/Jacob Carlborg


More information about the Digitalmars-d mailing list