(bug) false != EMPTY :: expression is always true

 

Hi,

MT4 build 1421

compiler build 2408

the expression:

if (state != EMPTY)
{
   Print("false != EMPTY");
}


triggers this message in the compiler:

expression is always true


But it is not valid. In the MT4 Tester everything works as it should. For example I have something like:

void func(bool state=EMPTY)
{
   if (state != EMPTY)
   {
       // Do something
   }
   else
   {
       // Do something else
   }
}

And I have been trying all three possibilities:

func(EMPTY);
func(true);
func(false);

And everything runs as expected.

So the message is false. Am I right?

 

Curious fact, reversing them:

if (EMPTY != state)
{
   // Do something
}

Does not trigger the message in the compiler

 
try to print EMPTY ;)
 
Cristian Dan Fechete #:

Curious fact, reversing them:

Does not trigger the message in the compiler

the "magic" is called:
auto type conversion
 
#property strict

void OnStart()
  {
   Print(typename(EMPTY));             // int
   Print("(int)true = ", (int)true);   // 1
   Print("(int)false = ", (int)false); // 0
   Print("EMPTY = ", EMPTY);           // -1
  }

EMPTY is a value of type int.

In your expression, the variable 'state' will be cast to type int, which will evaluate to either 0 or 1. Neither 0 nor 1 will ever equal -1.

The compiler correctly warns you: the expression is always true; the else statement is meaningless (control will never be transferred there), so the entire else statement will be cut out by the compiler at compile time.

 
Cristian Dan Fechete:

But it is not valid. In the MT4 Tester everything works as it should. For example I have something like:

void func(bool state=EMPTY)
{
   if (state != EMPTY)
   {
       // Do something
   }
   else
   {
       // Do something else
   }
}

And I have been trying all three possibilities:

func(EMPTY);
func(true);
func(false);

And everything runs as expected.

So the message is false. Am I right?

If you cast EMPTY to a bool type, you always get true.

void OnStart()
  {
   Print((bool)EMPTY); // true
  }

There is no problem inside the function, because it knows nothing about EMPTY. You implicitly cast EMPTY to type bool when you passed it to the function.

func(EMPTY) always means func(true)

 
Vladislav Boyko #:
There is no problem inside the function, because it knows nothing about EMPTY.

I didn't notice that in the function you also compare bool and int. The function also has a problem, of course. And the function also generates a warning.

 

Thank you all for the response.

It all started because I have been testing and:

if (EMPTY == true)
{
   // This will never run
   Print("EMPTY == true");
}

if (EMPTY == false)
{
   // This will never run either
   Print("EMPTY == false");
}


So I was hoping to use EMPTY as a third value for a bool type parameter.


It works great when evaluating against EMPTY, but it fails in functions like:
bool state = EMPTY;
ObjectSetInteger(_Chart_ID,name,OBJPROP_STATE,state);

@ObjectSetInteger it's used to update a BUTTON state on chart.

The result is the button will be pressed, so like some said, EMPTY evaluates to (bool)true, which in my case is not the desired result.


Wish there was strict comparison === available

 
Cristian Dan Fechete #:
So I was hoping to use EMPTY as a third value for a bool type parameter.

A variable (as well as a literal and an expression) of type bool will always be either true or false. If you need 3 or more values, you can declare an enum.