DIP6: Attributes

Ary Borenszweig ary at esperanto.org.ar
Mon Aug 3 08:39:08 PDT 2009


Daniel Keep wrote:
> 
> grauzone wrote:
>> Don wrote:
>>> Ary Borenszweig wrote:
>>>> http://www.prowiki.org/wiki4d/wiki.cgi?LanguageDevel/DIPs/DIP6
>>> This looks like a solution in search of a problem. What's the problem
>>> being solved?
>> Attaching additional data to types, that can't be specified otherwhere.
>> This should help with metaprogramming-like stuff.
>>
>> For example serialization. How do you specify that a field shouldn't be
>> part of the serialized data? Java has an extra keyword attribute like
>> "transient" (comes from before attributes were introduced). C# uses what
>> we call annotation in this thread. How would you do this in D?
> 
> struct Foo
> {
>     int serialise_me, dont_serialise_me, or_me;
> 
>     alias Tuple!("dont_serialise_me", "or_me") IgnoreForSerialisation;
> }
> 
> Or, if you'd rather have a less hacky interface:
> 
> struct Foo
> {
>     int serialise_me, dont_serialise_me, or_me;
> 
>     mixin IgnoreForSerialisation!("dont_serialise_me", "or_me");
> }

I can imagine this also:

struct Foo {
	char[] one;
         int two;
	float three;

	mixin SerializeAsXmlElement!("one", "desiredNameForOne", "two", 
"desiredNameForTwo");

	mixin SerializeAsXmlAttribute!("three", "desiredNameForThree");
}

That looks great. And as your struct grows, you have to scroll down to 
see how an element is serialized. And if you want to change how an 
element is serialized you scroll down and move two strings from one 
point to another. I also wonder *how* is that mixed template 
implemented, it probably adds some fields or things to that struct.

How about this:

struct Foo {

	@XmlElement("desiredNameForOne");
	char[] one;

	@XmlElement("desiredNameForTwo");
	int two;

	@XmlAttribute("desiredNameForThree");
	float three;
}

Which one looks nicer and more understandable to you? How do you change 
a property to be serialized as an element to an attribute? You just 
change "XmlElement" to "XmlAttribute" and that's it.

Your approach also is no DRY: you have to name the fields again. It 
might throw awful compiler errors for template code when you mistype 
something. If you rename a field you must also remember to rename the 
serialization mixin.



More information about the Digitalmars-d mailing list