Compile time function execution...
Brian Byrne
bdbyrne at wisc.edu
Fri Feb 16 15:17:32 PST 2007
I'm having a few problems getting some simple examples to work, for
instance:
-----
char[] foo() { return( "bar" ); }
void main() { const char[] bar = foo(); }
Assertion failure: 'parameters && parameters->dim == dim' on line 96 in
file 'interpret.c'
abnormal program termination
-----
and
-----
template eval( A... ) { alias A eval; }
int square( int n ) { return( n * n ); }
void main() { int bar = eval!( square( 5 ) ); }
Error: cannot implicitly convert expression (tuple25) of type (int) to int
-----
What am I doing wrong?
Thanks,
Brian Byrne
Walter Bright wrote:
> ... is now in DMD 1.006. For example:
>
>> -------------------------------------------
>> import std.stdio;
>>
>> real sqrt(real x)
>> {
>> real root = x / 2;
>> for (int ntries = 0; ntries < 5; ntries++)
>> {
>> if (root * root - x == 0)
>> break;
>> root = (root + x / root) / 2;
>> }
>> return root;
>> }
>>
>> void main()
>> {
>> static x = sqrt(10); // woo-hoo! set to 3.16228 at compile time!
>> writefln("%s, %s", x, sqrt(10)); // this sqrt(10) runs at run time
>> }
>> ------------------------------------------
>
> This should obsolete using templates to compute values at compile time.
More information about the Digitalmars-d
mailing list