digitalmars.D.learn - The problem with the value that is returned from the condition in
- Dennis Ritchie (31/31) Jun 06 2015 Hi,
- sigod (4/20) Jun 06 2015 So, I suppose it's should work without casting to bool or `!is`
- Dennis Ritchie (3/15) Jun 06 2015 I reported this:
- lobo (5/6) Jun 06 2015 `static if(5 in hash) {}` will not work because (5 in hash)
- lobo (5/11) Jun 06 2015 just to be clear, you cannot have a pointer to anything at
- Dennis Ritchie (5/18) Jun 07 2015 This, of course, it is logical, but it somehow works:
- "Marc =?UTF-8?B?U2Now7x0eiI=?= <schuetzm gmx.net> (11/24) Jun 07 2015 Not true:
- Dennis Ritchie (2/12) Jun 07 2015 Then I do not see any problems hindering satisfy my report :)
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?
Jun 06 2015
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.
Jun 06 2015
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:I reported this: https://issues.dlang.org/show_bug.cgi?id=14659Pulls 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.
Jun 06 2015
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
Jun 06 2015
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: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.[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
Jun 06 2015
On Sunday, 7 June 2015 at 03:04:38 UTC, lobo wrote:On Sunday, 7 June 2015 at 03:01:15 UTC, lobo wrote:This, of course, it is logical, but it somehow works: immutable hash = [1 : 3, 5 : 7]; static if (!!(5 in hash)) writeln("OK"); // prints OKOn Saturday, 6 June 2015 at 17:06:37 UTC, Dennis Ritchie wrote: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.[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
Jun 07 2015
On Sunday, 7 June 2015 at 03:04:38 UTC, lobo wrote:On Sunday, 7 June 2015 at 03:01:15 UTC, lobo 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());On Saturday, 6 June 2015 at 17:06:37 UTC, Dennis Ritchie wrote: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.[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
Jun 07 2015
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 :)
Jun 07 2015