Proposal: user defined attributes

dennis luehring dl.soluz at gmx.net
Sun Mar 18 23:46:21 PDT 2012


Am 19.03.2012 01:41, schrieb Walter Bright:
> I'm sorry, I find this *massively* confusing. What is foo? 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?

attributes does not containing code - there just a (at-runtime) 
queryable information that can be attached to serveral things (like 
classes, methods, ...) - think of it like double.epsilon - but 
extendable by users - thats it, and in the c# world these 
attribute-definitions tend to be something like an class (but without code)

in c# you can walk by (runtime)reflection through your code and find out 
if something is annotated with an special attribute and use the 
configure information and do something with it - call an constructor, 
open an connection, generated code (at runtime) - whatever you want

its a easy-to-use-buildin-attribution-system thats it - and people like 
them because c# do all the big magic by giving developers a bunch of 
attributes that are then used for stuff like serialization, 
memory-layout, ...

a compiletime example of this could be:

attribute my_special_attribute
{
    int version;
}

attribute my_special_attribute2
{
    string test;
}

class test
{
    [my_special_attribute(version=2)]
    int method1();

    [my_special_attribute(version=2)]
    [my_special_attribute2(test="bert")]
    int method2();
}

void main()
{
   auto b = [ __traits(allMembers, D) ];
   foreach( auto a; b )
   {
     -->attribute query magic
     -->auto c = [ __traits(attribute("my_special_attribute", a) ];
     -->auto c = [ __traits(attribute("my_special_attribute2", a) ];

     //now we know all methods with my_special_attribute
     //and speical_special_attribute2 and their content (version=2
     //and test="bert"

     //now think of an template or mixing that uses this
     //information for code-generation or something like that

     //thats all
   }
}






More information about the Digitalmars-d mailing list