Features of the mql5 language, subtleties and tricks - page 159

 
Yurixx:

Can you tell me please, pls.

In indicator the order of series, e.g. close[], is set by ArraySetAsSeries() once or in some other way ?

Is it done in OnCalculate() or can it be done in OnInit() ?

I've encountered a confusing situation:

Order in close[], set by AS_SERIES on entry on first tick, on next tick spontaneously changes to normal, i.e. !AS_SERIES.

I haven't found the reason for this in the code.

in any function getting scalar &arr[] you cannot be absolutely sure about the array indexing direction :-(

Moreover, if you change the "seriality" inside, this direction will remain after your function ends...it will be a side-effect, which no one expects

that's why, unfortunately, when you get an array for input into a function, remember its serialization, set it to a convenient one and ALWAYS return the original back when you exit the function

This is a rare case with OnCalculate, but in practice it used to be called from other code as well.

 
Maxim Kuznetsov:

Moreover, if you change "serialisation" internally, this direction will remain when your function ends...

That's what I was counting on. That's why I putArraySetAsSeries(close,true) inOnCalculate() block, which is executed only once at first login. And this "serialization" was indeed set. However, to my surprise, on the second tick, and beyond, the "seriality" was already the opposite.

Maxim Kuznetsov:

With OnCalculate it's a rare case, but in practice it happened to be called from other code as well.

I don't have such exotics and, moreover, I don't change the "serialization" of timeseries and buffer arrays inside the program. Therefore, it would be enough for me to set it once, at the beginning of the indicator. But, if we cannot be sure that the "seriality" of these arrays is saved, we have to set it at the beginning of each OnCalculate() loop. This seems to be something completely unnatural.

 
input string inStr = NULL; // Входная строка не может быть NULL, но об этом нигде не сообщается.

#define  PRINT(A) Print(#A + " = " + (string)(A));

void OnStart()
{
  string Str = NULL;
  
  PRINT(inStr == NULL); // false
  PRINT(Str == NULL);   // true


  PRINT(inStr == ""); // true
  PRINT(Str == "");   // false
}
It is a good idea to generate a warning at compile time.
 
fxsaber:
It is a good idea to generate a warning at compile time.

If nothing has changed since the old days, NULL != "" Many people have fallen for this before.

 
Alexey Viktorov:

If nothing has changed since the old days, NULL != "" Many people have fallen for this before.

That's not what we're talking about.

 
fxsaber:

We are talking about something else.

Then explain why it can't be. Why

input string inStr = "";

can be, but

input string inStr = NULL;

it can't be.

 
Alexey Viktorov:

Then explain why it can't be.

The script above shows this.

 
fxsaber:

The above script shows this.

If it did, there would be no questions. You always think that everyone around should read your mind, or be better trained than you in programming.

 
Alexey Viktorov:

If it did, there would be no questions. You always think that everyone should read your mind, or be better trained than you in programming.

I don't understand the reasons for this reaction. The terse code demonstrates the feature 100%.

 
fxsaber:

I don't understand the reasons for this reaction. The terse code demonstrates the feature 100%.

Normal reaction. I don't understand your codes, I asked for an explanation, and the answer is...

NULL is such an ambiguity that you have to deal with it carefully. Especially when applied to string variables.

From the documentation

//--- если строка не инициализирована, то присвоим ей наше предопределенное значение 
if(some_string==NULL) some_string="empty";

Hence, in this example NULL does not mean that the string length is equal to zero, but that the variable is not initialized.

In your example

input string inStr = NULL;

the variable is initialized. It is not clear to me how it is initialized and I don't feel like sorting it out.

Consequently,

PRINT(inStr == NULL); // false

Indicates that the variable is initialized. Again, with what is the big question. Why do you think it is impossible to initialize a variable by NULL?

Apparently this initialization causes the string length to be equal to zero, which is what this check says about

PRINT(inStr == ""); // true
Reason: