static if - is the 'static' really needed?

Nicolas Sicard dransic at gmail.com
Fri Dec 13 04:49:59 PST 2013


On Friday, 13 December 2013 at 12:10:02 UTC, comco wrote:
> Imagine a world in which a simple 'if' has the semantics of a 
> static if, if the condition is evaluable at CT. Is this a world 
> you would rather live in?
>
> template Fac(int i) {
> if (i == 0) { // static if; doesn't introduce scope
> enum Fac = 1;
> } else {
> enum Fac = i * Fac!(i-1);
> }
> }
>
> // If the condition is not evaluable at CT, the ordinary 
> runtime if semantics (introducing scope) are used.
>
> Me:
> pros: simpler syntax
> cons: harder to reason about; I recall Andrei's talk about the 
> static if proposal to C++: "we don't need _static else_" -- why 
> do we even need 'static' in 'static if' by this reasoning?

What would happen when the condition is sometimes evaluable at 
compile time and sometimes not?

void foo(alias a)() {
     /* static */ if (a)
         int x = 1;
     else
         int x = 42;
     doSomethingWith(x);
}


More information about the Digitalmars-d-learn mailing list