DIP6: Attributes

Max Samukha spambox at d-coding.com
Mon Aug 3 09:01:47 PDT 2009


On Tue, 04 Aug 2009 01:30:29 +1000, Daniel Keep
<daniel.keep.lists at gmail.com> 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");
>}

You can even use a mixin per member:

struct Foo
{
    mixin Persistent!("field", "field-as-it-is-named-in-db");
    int field;
    
    mixin Persistent!("field2");
    mixin Serializable!("field2"); 
    int field2;
}

It would be nice (and that is unlikely to happen) if the compiler
could rewrite

struct Foo
{
    @Persistent("db_field")
    @Serializable
    int field;
}

to

struct Foo
{
    int field;
    mixin Persistent!("field", "db_field");
    mixin Serializable!("field");
}






More information about the Digitalmars-d mailing list