Curious fact, reversing them:
if (EMPTY != state) { // Do something }
Does not trigger the message in the compiler
#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.
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)
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.
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
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Hi,
MT4 build 1421
compiler build 2408
the expression:
triggers this message in the compiler:
But it is not valid. In the MT4 Tester everything works as it should. For example I have something like:
And I have been trying all three possibilities:
And everything runs as expected.
So the message is false. Am I right?