The difference between extern and input - page 3

 
//+------------------------------------------------------------------+
//|                                                          inc.mqh |
//|                        Copyright 2017, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2017, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"

extern int x;

int z(){
   x=122;
   return x;

Expet:

//+------------------------------------------------------------------+
//|                                                          exp.mq5 |
//|                        Copyright 2017, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2017, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"

int x=1;

#include <inc.mqh>

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
   Alert(x);
   Alert(z());   
   Alert(x);   
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
   
  }
//+------------------------------------------------------------------+
 
Dmitry Fedoseev:

Duck, you have a library. Of course, there will be different variables. What is meant is to connect mqh via include.

In C++, externs are just described in include libraries, otherwise the compiler won't let you compile the project


Dmitry Fedoseev:

extern:

the includer will be inserted into the body of the program when you compile it. if you want to describe an instance there, the same thing will happen.


alternatively, you could describe an external class or structure with the modifier ;)

 
Igor Makanu:

In C++, externs are described in include libraries, otherwise the compiler will not let you compile the project.

The usual way to call a library is to use "#include" as the name of the file

 
Igor Makanu:

...

The point of this is that the includer will be inserted into the body of the program when compiling, if you want to describe an instance there, the same thing will happen.

The point is to allow the linkable file to be handled independently of the whole project, to compile it separately and thus check for errors.

 
Dmitry Fedoseev:

The point is to be able to work on a plug-in file independently of the whole project, to compile it separately and thereby check for errors.

I think we're looking for the point where it just isn't there.

the extern was retained for compatibility with thousands of previously written code and the help was written by a guy who copied some of the information from the wiki.

 
Igor Makanu:

I think we're looking for meaning where none exists.

imho, extern was retained for compatibility of thousands of codes written earlier, and the help was written by a person who copied some of the information from wiki

What's the verdict, gentlemen?) If I write an .mq5 program with .mqh (#include), I can safely write extern and not be afraid of unexpected consequences (as I did in mql4) or use input ?
 
Maksim Neimerik:
So what is the verdict of the Gentlemen?) If I write an .mq5 program with .mqh (#include), I can safely write extern and not be afraid of unexpected consequences (as I did in mql4) or use input ?

It depends on what purposes. They have different purposes.

 
Maksim Neimerik:
So what's the verdict gentlemen)? If I'm writing an .mq5 program with .mqh (#include), I can safely write extern and not be afraid of unexpected consequences (as I did in mql4) or should I still use input ?

Use the standard MQL code constructs: all input at the beginning of the code, then all include. This is how the examples from the developers are written, and 99% of code in codobase are written this way, and you won't get any surprises.

Once again about extern - they can be changed during the execution of the program, but in my opinion it is bad practice, usually all external variables in OnInit () are copied into their variables and then work with them (as an example of experts that work for 4-character and 5-character - examples of this design in the network tons)


And the bottom line - do not use extern, if you modify old code, replace it with input and the compiler will help you with warnings if there was a record in extern - you must fix it

 

By the way, I discovered that the compiler does not care that I can re-declare the enum content as a variable

enum mode5
{
   Manual         = 0,// Standard in Pips
   Kijun_sen      = 1 // Stoploss +/- Kijun-sen
};

//а потом где угодно можно объявить и компилятору будет пофиг

string Kijun_sen ="Да мне вообще пофиг что ты там объявил";      
 
Aleksey Semenov:

By the way, I found out that the compiler does not care that I can re-declare the enum content as a variable

what if I declare an enum variable?

enum mode5
{
   Manual         = 0,// Standard in Pips
   Kijun_sen      = 1 // Stoploss +/- Kijun-sen
};


string Kijun_sen ="Да мне вообще пофиг что ты там объявил"; 

void OnStart()
  {
      mode5 x = Kijun_sen;
   
  }
//+------------------------------------------------------------------+

implicit enum conversion tst1.mq5 24 17


you can also check EnumToString()


I think the type without declared variables was just thrown out of the compilation as not used

Reason: