![MQL5 - Language of trade strategies built-in the MetaTrader 5 client terminal](https://c.mql5.com/i/registerlandings/logo-2.png)
You are missing trading opportunities:
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
Registration
Log in
You agree to website policy and terms of use
If you do not have an account, please register
So what is the point of specifying default:return(false ) if the variable belongs to the bool type, which in principle has only two values, both of which are used in the switch statement? In other words, the default label should never be executed at all.
P.S. 1. Also, if we follow your approach, then the default label becomes mandatory instead of optional.
2. break operators are meaningless in both cases.
The switch operator converts your bool_var to an integer type.
And the compiler doesn't have to know that this variable has two values. It's not analyzing the algorithm logically.
You forget one more thing - any variable can have an invalid value or be uninitialised (have a "rubbish" value)...
I don't forget. So in my example the variable was initialized. Despite this, the compiler generated an error.
The example withbool_var=-1is extremely incorrect. See it for yourself:
The switch statement converts your bool_var to an integer type.
And the compiler doesn't need to know that this variable has two values. It is not analyzing the algorithm logically.
By the way, with booleans, there is a lucky variant when an uninitialized variable or a variable with value -1, for instance, will be cast to true/false.
Which, of course, won't happen if you explicitly declare the variable as int.
I don't forget. That's why the variable was initialized in my example. Despite this, the compiler generated an error.
The example withbool_var=-1is extremely incorrect. Convince yourself:
I'm already convinced of it and that's why I've removed it. But it's an accidental luck (consider it as a feature of the language).
But when handling a possible error in a switch block, the compiler doesn't have to figure out what and how is being input there, so the compiler treats it as an error, not a warning.
And the compiler doesn't need to know that this variable has two values. It's not doing a logical analysis of the algorithm.
So you're saying that the compiler doesn't take into account the list of values in the enumeration and their total number?
But when processing a possible error in a switch block, the compiler doesn't have to figure out what and how it's being input, so the compiler treats it as an error, not a warning.
1. bool is already an "integer type". Or are you saying that switch is a conversion to a higher integer type.
2. Are you saying that the compiler does not take into account the list of enum values and their total number?
1. bool is an integer type with the size of one byte. The conversion is to int type, you're right.
2. You are right here too. Only case is not an enumeration, but just a label for conditional branching.
1. bool is a one byte integer type. The conversion goes to int type, you are right.
According to the Handbook, bool is a special type other than integer... So I deleted my incorrect statement. Although I'm not going to argue - I'm not an expert.
Dima_S:
So you mean that the compiler doesn't take into account the list of values from the enumeration and their total number?
2. You've written it correctly here too. Only case is not an enumeration but just a label for a conditional branch.
I meant not case labels but enumerations (considering bool type as the smallest one). Here is an example with the same compilation error:
So I repeat my question concerning this example: do you mean that the compiler doesn't take into account the list of values from the Triple enumeration and their total number? I have all enum values used in the switch operator.
The intermediate conclusion is as follows: using default label becomes mandatory when using enumeration+switch?
Let's imagine this way (by the way, value 3 does not work with explicit declaration of a value, but -1 does). But absence of explicit variable initialization will show that the value is equal to the one with 0 index (all this is well thought out).
What should the function return if the default option is excluded?
The intermediate conclusion is as follows: using default label becomes mandatory when using enumeration+switch?
yes, if something needs to be returned in return().
or at least so that the compiler can be sure that the function will return something: