www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - The problem with the value that is returned from the condition in

reply "Dennis Ritchie" <dennis.ritchie mail.ru> writes:
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
next sibling parent reply "sigod" <sigod.mail gmail.com> writes:
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
parent "Dennis Ritchie" <dennis.ritchie mail.ru> writes:
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
Jun 06 2015
prev sibling parent reply "lobo" <swamplobo gmail.com> writes:
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
parent reply "lobo" <swamplobo gmail.com> writes:
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.
Jun 06 2015
next sibling parent "Dennis Ritchie" <dennis.ritchie mail.ru> writes:
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
Jun 07 2015
prev sibling parent reply "Marc =?UTF-8?B?U2Now7x0eiI=?= <schuetzm gmx.net> writes:
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());
Jun 07 2015
parent "Dennis Ritchie" <dennis.ritchie mail.ru> writes:
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