compile-time regex redux

Frits van Bommel fvbommel at REMwOVExCAPSs.nl
Thu Feb 8 00:34:35 PST 2007


Kyle Furlong wrote:
> import std.metastrings;
> 
> template ManyStructs(int count)
> {
>     static if(count == 1)
>     {
>         const char[] ManyStructsImpl = "struct Struct" ~ 
> ToString!(count) ~ " { int baz = "~ToString!(count)~"; }";
>     }
>     else
>     {
>         const char[] ManyStructsImpl = "struct Struct" ~ 
> ToString!(count) ~ " { int baz = "~ToString!(count)~"; 
> }"~ManyStructsImpl!(count - 1);
>     }
> }
> 
> mixin(ManyStructs!());
> 
> Clearly the former is much more succinct.

Ending the loop at 0 instead of 1:
---
import std.metastrings;

template ManyStructs(int count)
{
     static if(count == 0)
     {
         const char[] ManyStructsImpl = "";
     }
     else
     {
         const char[] ManyStructsImpl = "struct Struct" ~ 
ToString!(count) ~ " { int baz = "~ToString!(count)~"; 
}"~ManyStructsImpl!(count - 1);
     }
}
---
Duplicate code is bad :P.



More information about the Digitalmars-d mailing list