Compile time function execution...

Walter Bright newshound at digitalmars.com
Thu Feb 15 14:09:13 PST 2007


Andrei Alexandrescu (See Website For Email) wrote:
> A simple question is: what is the signature of regexmatch? A
> runtime-only version is:
> 
> bool regexmatch_1(char[] input, char[] pattern);
> 
> A compile-time-only version is:
> 
> bool regexmatch_2(pattern : char[])(char[] input);
> 
> Notice how the two cannot be called the same way. So the burden is on
> the user to specify different syntaxes for the two cases:
> 
> bool b1 = regexmatch_1(a, ".* = .*"); // forced runtime
> bool b2 = regexmatch_2!(".* = .*")(a); // forced compile-time
> 
> Notice that b2 is NOT computed at compile time!!! This is because "a" is
> a regular variable. It's just that the code for computing b2 is
> radically different from the code for computing b1 because the former
> uses static knowledge of the pattern.
> 
> The problem is that what's really needed is this:
> 
> bool b = regexmatch(string, pattern);
> 
> and have regexmatch dispatch to regexmatch_1 if pattern is a variable,
> or to regexmatch_2 if pattern is a compile-time constant.
> 
> Do you feel me?

No, but I understand your point.

> What we need is allow a means to overload a function with a template, in
> a way that ensures unified invocation syntax, e.g.:
> 
> bool regexmatch(char[] str, char[] pat); // 1
> bool regexmatch(char[] pat)(char[] str, pat); // 2
> bool regexmatch(char[] pat, char[] str)(str, pat); // 3
> 
> void main(int argc, char[][] argv)
> {
>   regexmatch(argv[0], argv[1]); // goes to (1)
>   regexmatch(argv[0], ".+"); // goes to (2)
>   regexmatch("yah", ".+"); // goes to (3)
> }
> 
> Notice how the invocation syntax is identical --- an essential artifact.

Remember your proposal for a expression type which resolves to "does 
this expression compile"? That can be used here, along with expression 
aliasing, to test to see if it can be done at compile time, and then 
pick the right fork.



More information about the Digitalmars-d mailing list