Proposal: user defined attributes

Tove tove at fransson.se
Mon Mar 19 01:01:32 PDT 2012


On Monday, 19 March 2012 at 06:46:09 UTC, dennis luehring wrote:
> 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
>   }
> }

Well I was thinking if we can go one step further than C#, 
because of D:s CTFE... by introducing a call back from the D 
compiler to the library CTFE attribute handler... this way we can 
hide all reflection from the "end user"... and the compiler 
doesn't need to know anything at all, as the library does the 
semantic lowering.

@GC.NoScan int value;
@GC this() {}

Compiler Asks library for transformation of unknown @GC
bool library("@GC this() {anything...}") ->

if the library succeeds it would then transform the string and 
hand back a lowered mixin to the compiler.

mixin("
this()
{
   auto b = [ __traits(allMembers, D) ];
   foreach( auto a; b) {DO.GC.Stuff...}

   anything...
}
");




More information about the Digitalmars-d mailing list