Compile time function execution...

Andrei Alexandrescu (See Website For Email) SeeWebsiteForEmail at erdani.org
Thu Feb 15 14:16:42 PST 2007


Walter Bright wrote:
> Andrei Alexandrescu (See Website For Email) wrote:
>> 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.

It could indeed; I'm just hoping it can be properly cloaked away (e.g. 
in a library) at little cognitive cost to both the user and library 
developer. I assume it will be something often asked for.

Many Perl coders probably have no idea that:

$a =~ ".*=.*";

is faster than, and handled very, very differently, from:

$a =~ $b;

where $b is a dynamic variable. They just use the uniform syntax and let 
the compiler do whatever the hell it has to do to generate good code. We 
should make that kind of partial evaluation easy to define and implement.


Andrei



More information about the Digitalmars-d mailing list