digitalmars.D.learn - static if - is the 'static' really needed?
- comco (17/17) Dec 13 2013 Imagine a world in which a simple 'if' has the semantics of a
- Nicolas Sicard (10/27) Dec 13 2013 What would happen when the condition is sometimes evaluable at
- comco (15/45) Dec 13 2013 Multiple versions of the foo function - depending on the caller -
- Jesse Phillips (3/13) Dec 13 2013 I would not rather live in it. I would rather live in:
- Chris Cain (10/15) Dec 13 2013 They are fundamentally two different things. Eliding the
- Parke (3/5) Dec 14 2013 Even so, I would still want static if, so that I would get a compile
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?
Dec 13 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); }
Dec 13 2013
On Friday, 13 December 2013 at 12:50:01 UTC, Nicolas Sicard wrote:On Friday, 13 December 2013 at 12:10:02 UTC, comco wrote:Multiple versions of the foo function - depending on the caller - as it is with templates? Static ifs already can produce a combinatorial explosion of different method implementations :) At least in the places where declarations are expected and normal if doesn't make sense: like template/class/struct body, if can be implicitly static. Now it is implicitly static right after function / class / template declarations. class A(T) { if (is(T : B)) B b; else T b; ... }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); }
Dec 13 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); } }I would not rather live in it. I would rather live in: static foreach(...) // but that hasn't happened yet.
Dec 13 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?They are fundamentally two different things. Eliding the difference is not a good idea. I'm with Jesse, I wish we had a "static foreach" because it's also a significant difference as well."we don't need _static else_" -- why do we even need 'static' in 'static if' by this reasoning?Not exactly sure what the context of this is, but I suspect he was saying "we don't need _static else_" because we don't need it to resolve ambiguities in the AST (which is true). OTOH we would need 'static' in 'static if' to resolve the ambiguities in the semantics.
Dec 13 2013
On Fri, Dec 13, 2013 at 4:10 AM, comco <void.unsigned gmail.com> 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?Even so, I would still want static if, so that I would get a compile time error if I mistaken thought code could be evaluated at CT.
Dec 14 2013