Template-based Preprocessing

Garett Bass garettbass at studiotekne.com
Mon Sep 4 18:49:22 PDT 2006


Walter Bright wrote:
> Not a bad idea, but it would require a lot of internal rewriting of the 
> compiler to implement.

Rewriting the front-end only or the back-end too?  I'm disappointed to hear that it is so much work; this would really wrap up the boilerplate for my serialization library:

   template Serializable(T, char[] I) {
       T identifier(I);
          string identifier("serialize_" ~ I)() {
           return toString(identifier(I));
       }
   }

   class Foo {
       mixin Serializable!(int, "i");
   }

Foo should expand to the equivalent of:

   class Foo {
       int i;

       string serialize_i() {
           return toString(i);
       }
   }

Of course there would also be a static registrar that would register the dynamically generated serialization and deserialization delegates at the class level at static initialization time, and associate them by string.  With a couple other components, this allows serializable classes enough reflection to serialize and deserialize their members and properties.



More information about the Digitalmars-d mailing list