The problem with the value that is returned from the condition in `static if`. Bug or feature?

Dennis Ritchie via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sat Jun 6 10:06:36 PDT 2015


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?


More information about the Digitalmars-d-learn mailing list