compile-time regex redux
Kyle Furlong
kylefurlong at gmail.com
Wed Feb 7 21:34:58 PST 2007
Miles wrote:
> Walter Bright wrote:
>> The problem is that parsing strings using templates generates a large
>> number of template instantiations, is (relatively) very slow, and
>> consumes a lot of memory (at compile time, not runtime). For example,
>> ParseInteger would need 4 template instantiations to parse 5678,
>
> Why instead of doing perversions with templates, don't you add a proper
> compile-time D interpreter for the purposes of generating code? Doing
> loops with template recursion sucks a lot. It really looks the wrong
> approach for the problem.
>
> D already has static if() and tuple-foreach(). Just adding compile-time
> variables and a static for() will be great. A compile-time D interpreter
> will be awesome.
As an example of this, I recently was playing with the new mixins, and
wanted to mixin an n amount of structs. I quickly realized that this
would not be as trivial as
static for(int i = n; i < n; i++)
{
mixin("my struct code");
}
but ended up being:
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.
More information about the Digitalmars-d
mailing list