Avoid compile time evaluation
Simen Kjaeraas
simen.kjaras at gmail.com
Fri Apr 13 06:01:02 PDT 2012
On Fri, 13 Apr 2012 14:52:05 +0200, Andrea Fontana <nospam at 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.
More information about the Digitalmars-d-learn
mailing list