Thread overview | ||||||||
---|---|---|---|---|---|---|---|---|
|
December 13, 2013 static if - is the 'static' really needed? | ||||
---|---|---|---|---|
| ||||
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? |
December 13, 2013 Re: static if - is the 'static' really needed? | ||||
---|---|---|---|---|
| ||||
Posted in reply to comco | 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);
}
|
December 13, 2013 Re: static if - is the 'static' really needed? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Nicolas Sicard | 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:
>> 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);
> }
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;
...
}
|
December 13, 2013 Re: static if - is the 'static' really needed? | ||||
---|---|---|---|---|
| ||||
Posted in reply to comco | 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.
|
December 13, 2013 Re: static if - is the 'static' really needed? | ||||
---|---|---|---|---|
| ||||
Posted in reply to comco | 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. |
December 14, 2013 Re: static if - is the 'static' really needed? | ||||
---|---|---|---|---|
| ||||
Posted in reply to comco | 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.
|
Copyright © 1999-2021 by the D Language Foundation