Thread overview | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
June 06, 2015 The problem with the value that is returned from the condition in `static if`. Bug or feature? | ||||
---|---|---|---|---|
| ||||
Hi, No wonder that it works, because it is the legacy C++ (and I like that everything is different from zero is true): if (5) writeln("OK"); // prints OK In the `static if` this condition also works perfectly: static if (5) writeln("OK"); // prints OK Here idiomatic version check in the hash key: int[int] hash = [1 : 3, 5 : 7]; if (hash.get(5, false)) writeln("OK"); // prints OK In the `static if` this condition also works perfectly: immutable hash = [1 : 3, 5 : 7]; static if (hash.get(5, false)) writeln("OK"); // prints OK It's not quite idiomatic version to check if a key in the hash: int[int] hash = [1 : 3, 5 : 7]; if (5 in hash) writeln("OK"); // prints OK The problem is that the `static if` it does not work: immutable hash = [1 : 3, 5 : 7]; static if (5 in hash) writeln("OK"); // Error: expression &[1:3, 5:7][5] // is not constant or does not evaluate to a bool You have to write something like that :) immutable hash = [1 : 3, 5 : 7]; static if (!!(5 in hash)) writeln("OK"); // prints OK Pulls whether this issue? Or is it normal? |
June 06, 2015 Re: The problem with the value that is returned from the condition in `static if`. Bug or feature? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Dennis Ritchie | On Saturday, 6 June 2015 at 17:06:37 UTC, Dennis Ritchie wrote: > The problem is that the `static if` it does not work: > > immutable hash = [1 : 3, 5 : 7]; > > static if (5 in hash) > writeln("OK"); > // Error: expression &[1:3, 5:7][5] > // is not constant or does not evaluate to a bool > > You have to write something like that :) > > immutable hash = [1 : 3, 5 : 7]; > > static if (!!(5 in hash)) > writeln("OK"); // prints OK > > Pulls whether this issue? Or is it normal? http://dlang.org/version.html#staticif: > StaticIfCondition: > static if ( AssignExpression ) > > AssignExpression is implicitly converted to a boolean type, and is evaluated at compile time. The condition is satisfied if it evaluates to true. It is not satisfied if it evaluates to false. So, I suppose it's should work without casting to bool or `!is` operator. |
June 06, 2015 Re: The problem with the value that is returned from the condition in `static if`. Bug or feature? | ||||
---|---|---|---|---|
| ||||
Posted in reply to sigod | On Saturday, 6 June 2015 at 18:16:28 UTC, sigod wrote: > On Saturday, 6 June 2015 at 17:06:37 UTC, Dennis Ritchie wrote: >> Pulls whether this issue? Or is it normal? > > http://dlang.org/version.html#staticif: > >> StaticIfCondition: >> static if ( AssignExpression ) >> >> AssignExpression is implicitly converted to a boolean type, and is evaluated at compile time. The condition is satisfied if it evaluates to true. It is not satisfied if it evaluates to false. > > So, I suppose it's should work without casting to bool or `!is` operator. I reported this: https://issues.dlang.org/show_bug.cgi?id=14659 |
June 07, 2015 Re: The problem with the value that is returned from the condition in `static if`. Bug or feature? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Dennis Ritchie | On Saturday, 6 June 2015 at 17:06:37 UTC, Dennis Ritchie wrote:
> [snip]
`static if(5 in hash) {}` will not work because (5 in hash) returns a pointer to the value or null if the key oesn't exist.
bye,
lobo
|
June 07, 2015 Re: The problem with the value that is returned from the condition in `static if`. Bug or feature? | ||||
---|---|---|---|---|
| ||||
Posted in reply to lobo | On Sunday, 7 June 2015 at 03:01:15 UTC, lobo wrote:
> On Saturday, 6 June 2015 at 17:06:37 UTC, Dennis Ritchie wrote:
>> [snip]
>
> `static if(5 in hash) {}` will not work because (5 in hash) returns a pointer to the value or null if the key oesn't exist.
>
> bye,
> lobo
just to be clear, you cannot have a pointer to anything at compile time because it doesn't exist and IMO changing 'in' to behave differently for static-if compared runtime if would be bad.
|
June 07, 2015 Re: The problem with the value that is returned from the condition in `static if`. Bug or feature? | ||||
---|---|---|---|---|
| ||||
Posted in reply to lobo | On Sunday, 7 June 2015 at 03:04:38 UTC, lobo wrote:
> On Sunday, 7 June 2015 at 03:01:15 UTC, lobo wrote:
>> On Saturday, 6 June 2015 at 17:06:37 UTC, Dennis Ritchie wrote:
>>> [snip]
>>
>> `static if(5 in hash) {}` will not work because (5 in hash) returns a pointer to the value or null if the key oesn't exist.
>>
>> bye,
>> lobo
>
> just to be clear, you cannot have a pointer to anything at compile time because it doesn't exist and IMO changing 'in' to behave differently for static-if compared runtime if would be bad.
This, of course, it is logical, but it somehow works:
immutable hash = [1 : 3, 5 : 7];
static if (!!(5 in hash))
writeln("OK"); // prints OK
|
June 07, 2015 Re: The problem with the value that is returned from the condition in `static if`. Bug or feature? | ||||
---|---|---|---|---|
| ||||
Posted in reply to lobo | On Sunday, 7 June 2015 at 03:04:38 UTC, lobo wrote:
> On Sunday, 7 June 2015 at 03:01:15 UTC, lobo wrote:
>> On Saturday, 6 June 2015 at 17:06:37 UTC, Dennis Ritchie wrote:
>>> [snip]
>>
>> `static if(5 in hash) {}` will not work because (5 in hash) returns a pointer to the value or null if the key oesn't exist.
>>
>> bye,
>> lobo
>
> just to be clear, you cannot have a pointer to anything at compile time because it doesn't exist and IMO changing 'in' to behave differently for static-if compared runtime if would be bad.
Not true:
immutable y = 1;
enum x = &y;
You can even do pointer arithmetics:
auto foo() {
auto x = [1,2,3,4];
auto y = &x[1];
return y[2];
}
pragma(msg, foo());
|
June 07, 2015 Re: The problem with the value that is returned from the condition in `static if`. Bug or feature? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Marc Schütz | On Sunday, 7 June 2015 at 11:33:56 UTC, Marc Schütz wrote:
> Not true:
>
> immutable y = 1;
> enum x = &y;
>
> You can even do pointer arithmetics:
>
> auto foo() {
> auto x = [1,2,3,4];
> auto y = &x[1];
> return y[2];
> }
> pragma(msg, foo());
Then I do not see any problems hindering satisfy my report :)
|
Copyright © 1999-2021 by the D Language Foundation