static-if cant compile

Adam D. Ruppe via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sun Jul 19 05:22:16 PDT 2015


On Sunday, 19 July 2015 at 12:17:04 UTC, Clayton wrote:
> Pardon me for trivial question, Am new to D.

In D, you can run regular runtime code at compile time in a lot 
of cases. Just write an ordinary function that returns the data 
you need, then use it in a static variable initialization.

// example function, notice regular if and variables inside
int calculate(int i) { if(i < 0) return -i; else return i; }


static int value = calculate(5); // this is run at compile time



Ordinary functions will be interpreted if asked for their result 
in a context that only can be run at compile time, like a static 
variable initialization or inside a static if.


More information about the Digitalmars-d-learn mailing list