Errors, bugs, questions - page 416

 
Yedelkin:

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.

 
Interesting:

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:

bool bool_var=-1;
void OnStart()
  {
   Print("bool_var=",bool_var);
  }   
 
Dima_S:

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.

Yedelkin:

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.

 
Dima_S:

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?

 
Interesting:

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.

The intermediate conclusion is as follows: using the default label becomes mandatory when using enumerations+switch?
 
Yedelkin:

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.

Документация по MQL5: Основы языка / Типы данных / Целые типы / Типы char, short, int и long
Документация по MQL5: Основы языка / Типы данных / Целые типы / Типы char, short, int и long
  • www.mql5.com
Основы языка / Типы данных / Целые типы / Типы char, short, int и long - Документация по MQL5
 
Dima_S:

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:

Yedelkin:

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:

enum Triple
  {
   err=-1,
   no = 0,
   hay= 1
  };
Triple triple_var=err;
Triple Test(void)
  {
   switch(triple_var)
     {
      case  err: return(err);
      case   no: return(no);
      case  hay: return(hay);
      //default:return(hay);
     }
  }
void OnStart()
  {
   Test();
  }

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.

 
Yedelkin:
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?

ENUM_CHART_MODE ChartMode = -1;

string Test()
  {
   switch(ChartMode)
     {
      case 0: return("Bars"); break;
      case 1: return("CANDLES"); break;
      case 2: return("LINE"); break;      
      default: return("Unknown");
     }
  }
void OnStart()
{
Comment(Test());
}
 
Yedelkin:
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:

bool bool_var=false;
bool Test(void)
  {
   switch(bool_var)
     {
      case  true: return(true);
      case false: return(false);
     }
    return(false);//хотя до сюда ни когда не дойдем, но прописать надо
  }
void OnStart()
  {
   Test();
  }
 
Thank you all for the science! I'll have to grind my teeth to put in extra lines :)
Reason: