Suggestion: "break template;"
Russell Lewis
webmaster at villagersonline.com
Wed Feb 28 13:35:34 PST 2007
I have been writing a compile-time parser using Tuples and CTFE. I
admit, I am writing it basically by thinking iteratively, and writing
templates that execute the code that would have otherwise happened at
runtime.
There is one big annoyance I keep running into, though, and it is the
inability to "return" from this sort of template in the middle. Imagine
that I'm thinking something like this (writing it as runtime code, first):
parse_tree parse(char[] str) {
if(str.length == 0)
return new parse_tree; // empty tree
// do lots of other complex stuff
}
Now, when I translate this into templates, I get something like:
template Parse(char[] str) {
static if(str.length == 0)
alias struct {} Parse;
else {
// do lots of other complex stuff
}
}
Sometimes, I end up pretty deeply nested in the else clauses of my
static if's. It's technically correct, but ugly. What I would like to
see is the template equivalent of "return" - something which would break
you out of the current evaluation. My idea is to use the syntax "break
template;"
Then the template code becomes something which I think is clearer, and
closer to how I'm actually thinking about things:
template Parse(char[] str) {
static if(str.length == 0) {
alias struct {} Parse;
break template;
}
// do lots of other complex stuff
}
Russ
More information about the Digitalmars-d
mailing list