www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Avoid compile time evaluation

reply "Andrea Fontana" <nospam example.com> writes:
If I have something like:

static int var = myFunction();

dmd will evaluate myFunction() at compile time. If it can't, it 
gives me a compile error, doesn't it? If I'm not wrong, static 
force this.

If i don't use static, dmd will try to evaluate myfunction() at 
compile time, and if it can't, myfunction() will be executed at 
runtime, right?

So I have a code like this:

...
// Here some code to debug/fix...
// Here some code to debug/fix...
// Here some code to debug/fix...
// Here some code to debug/fix...
...
static int var = myVeryVeryComplexFunction();

If i have to work to some code before my complex function, every 
time I have to re-compile code, it takes a lot because dmd 
evalute at compile time myVeryVeryComplexFunction() also if i 
don't use static. Does a keyword to force runtime evaluation 
exists? I can't find any documentation (neither on static used in 
this way, any link?)...

My dirty way to do this is to edit myVeryVeryComplexFunction() 
adding a writeln() (or something similar) to function body.
Apr 13 2012
parent reply "Simen Kjaeraas" <simen.kjaras gmail.com> writes:
On Fri, 13 Apr 2012 14:52:05 +0200, Andrea Fontana <nospam example.com>  
wrote:

 If I have something like:

 static int var = myFunction();

 dmd will evaluate myFunction() at compile time. If it can't, it gives me  
 a compile error, doesn't it? If I'm not wrong, static force this.
Indeed.
 If i don't use static, dmd will try to evaluate myfunction() at compile  
 time, and if it can't, myfunction() will be executed at runtime, right?
No. static or enum forces evaluation at compiletime, otherwise it's runtime. Conceivably, the compiler could try running every function at compile-time as an optimization, but that would completely destroy the nice compilation times we like to brag about, and likely also break a lot of code.
 So I have a code like this:

 ...
 // Here some code to debug/fix...
 // Here some code to debug/fix...
 // Here some code to debug/fix...
 // Here some code to debug/fix...
 ...
 static int var = myVeryVeryComplexFunction();

 If i have to work to some code before my complex function, every time I  
 have to re-compile code, it takes a lot because dmd evalute at compile  
 time myVeryVeryComplexFunction() also if i don't use static. Does a  
 keyword to force runtime evaluation exists? I can't find any  
 documentation (neither on static used in this way, any link?)...
See above. Runtime evaluation is the default, and compile-time needs to be forced.
Apr 13 2012
parent "Andrea Fontana" <nospam example.com> writes:
That's strange, so why writeln make it compile faster? :)
I can't post the code, i'll try to reproduce it...

On Friday, 13 April 2012 at 13:01:03 UTC, Simen Kjaeraas wrote:
 On Fri, 13 Apr 2012 14:52:05 +0200, Andrea Fontana 
 <nospam example.com> wrote:

 If I have something like:

 static int var = myFunction();

 dmd will evaluate myFunction() at compile time. If it can't, 
 it gives me a compile error, doesn't it? If I'm not wrong, 
 static force this.
Indeed.
 If i don't use static, dmd will try to evaluate myfunction() 
 at compile time, and if it can't, myfunction() will be 
 executed at runtime, right?
No. static or enum forces evaluation at compiletime, otherwise it's runtime. Conceivably, the compiler could try running every function at compile-time as an optimization, but that would completely destroy the nice compilation times we like to brag about, and likely also break a lot of code.
 So I have a code like this:

 ...
 // Here some code to debug/fix...
 // Here some code to debug/fix...
 // Here some code to debug/fix...
 // Here some code to debug/fix...
 ...
 static int var = myVeryVeryComplexFunction();

 If i have to work to some code before my complex function, 
 every time I have to re-compile code, it takes a lot because 
 dmd evalute at compile time myVeryVeryComplexFunction() also 
 if i don't use static. Does a keyword to force runtime 
 evaluation exists? I can't find any documentation (neither on 
 static used in this way, any link?)...
See above. Runtime evaluation is the default, and compile-time needs to be forced.
Apr 13 2012