Discussing the article: "Master MQL5 from beginner to pro (Part II): Basic data types and use of variable" - page 2

 
Oleh Fedorov #:

Ahem... I'm embarrassed to ask, but I have to....

  • How often do such constructs occur in real MQL5 code?
  • "June interview"... Does it mean that you personally have met large companies, one of whose main areas of activity is MQL? (Personally, I have not had to date).
  • And will this code produce the same results in all c-like languages?

In short, why does a neophyte need such a deep understanding of literals?


P.S. I personally, though not a neophyte, failed your test without answering a single question correctly. I met something similar in textbooks - either in Java or in PHP - but it was so long ago, and it doesn't correspond to the practical code I had to read or write until now... I'm not going to be a june in your office. And... what?

P.P.S. I think that if you write an article about literals, it might be useful not only for me. ;-) Especially if you share your experience, which is not in the help.... However, undocumented features usually embarrass me a lot, because they change too often in new versions, but, you never know.... Maybe it will be really important or at least useful?

You see. The question is not how often it happens, but that people don't know and don't want to know the subtleties. And about bringing in types, you need to know that because it's the foundation of the basics. You get so much pain through it. By the way, this code is not about type conversion, but for you to think about:

enum E{
   int a;
   float b;
}

double F(E& e, int a){
   e.a = a;
   return e.b;
}

So, based on the help, everything is fine here. Only, for me personally, there is a nuance. According to the developers' claims, mql is written in C++, and in C++ you can't do that, it's UB, but in C everything is legal. I have seen such code on pluses often, and yes it worked and works, but personally I am somehow scared to write like that, but whether mql developers are scared, I don't know. That's why I will never do this in mql, because I don't know the implementation.

 
Vladimir Simakov #:


According to the developers' claims, mql is written in C++, and in C++ you can't do that, it's UB, but in C everything is legal.

Excuse me, but the developers claim that the MQL programming language is as close as possible to C++, not written in it. ))

Regards, Vladimir.

 
Vladimir Simakov #:
And about type conversion, it is necessary to know it, as it is the basis of the basics.

And I won't even try to argue about it. I totally agree. It's just... Well, not all C constructs are equally useful in MQL! :-)

Regarding the code "to think about"... Since I am not familiar with C (both of them), this code looks like a game to me. In the help, the enum type is referred to integer types, and I didn't even think that it is a structure at all. Its meaning is quite different... But thanks, now I'll know it - although it's of no use to me personally, I won't use it at all in this form.

So, based on the help, everything is normal here.

Can I have a link?

Документация по MQL5: Основы языка / Типы данных / Целые типы / Перечисления
Документация по MQL5: Основы языка / Типы данных / Целые типы / Перечисления
  • www.mql5.com
Данные перечислимого типа enum относятся к некоторому ограниченному множеству данных. Определение перечислимого типа: Список значений представляет...
 
Oleh Fedorov #:

Regarding the code "to think about"... Since I am not familiar with C (both of them), this code looks like a game to me. In the help, the enum type is referred to integer types, and I didn't even think that it is a structure at all. Its meaning is quite different... But thanks, now I'll know - although it's of no use to me personally, I won't use it :-).

This is the game. A person must have written from the ceiling and mixed up enum and union. His example does not compile. Enum is not a structure but a 4-byte integer. If you take union, it works perfectly well in C++ and MQL.

PS. Regarding the first example from Jun's interview and the question about how often it happens - of course it doesn't happen in pure form, because the example is refined, but very often signed and unsigned integers are used in code mixed together, and problems are quite probable here.
 
Stanislav Korotky #:

This is nonsense. The person must have written from the ceiling and mixed up enum and union. His example does not compile. Enum is not a structure but a 4-byte integer. If you take union, it works perfectly well in C++ and MQL.

PS. Regarding the first example from Jun's interview and the question about how often it happens - of course it doesn't happen in its pure form, because the example is refined, but very often signed and unsigned integers are used in code mixed together, and here problems are quite likely.

Naturally union)))))

About the rest: https: //en.cppreference.com/w/cpp/language/union

It is undefined behavior to read from the member of the union that wasn't most recently written. 

And yes, I know what it says next

Many compilers implement, as a non-standard language extension, the ability to read inactive members of a union.

Except that msvs, from whose help it is taken, doesn't claim the second one, though nobody has come across it yet. g++ and clang haven't looked at what they say about it, but it's hardly different.

If you want to use it that way, use it - it's your right and your pain, if anything happens))).

 
Vladimir Simakov #:

Naturally))))

About the rest: https: //en.cppreference.com/w/cpp/language/union

cppreference is a useful resource, but due to its reference nature it cannot contain all the nuances that can be found only in language specifications. To generalise them, it is easier to look at stackoverflow, and as a summary - for primitive types, bitwise "transfer" of values is guaranteed when reading a field, even if it has not been written to.

Accessing inactive union member and undefined behavior?
Accessing inactive union member and undefined behavior?
  • 2012.07.07
  • Luchian Grigore Luchian Grigore 257k 66 66 gold badges 461 461 silver badges 626 626 bronze badges
  • stackoverflow.com
I was under the impression that accessing a member other than the last one set is UB, but I can't seem to find a solid reference (other than answers claiming it's UB but without any support from the standard). So, is it undefined behavior?
 
Stanislav Korotky #:

This is nonsense. A person must have written from the ceiling and mixed up enum and union. His example does not compile. Enum is not a structure but a 4-byte integer. If you take union, it works perfectly well in C++ and MQL.

PS. Regarding the first example from Jun's interview and the question about how often it happens - of course it doesn't happen in its pure form, because the example is refined, but very often signed and unsigned integers are used in code mixed together, and problems are quite probable here.

Phew, the world picture is restored! :-) I'm just finishing about unions and other complex types (hopefully, if there are no more blunders, it should be out next week).

P.S. MetaEditor does not highlight literals with suffixes `u` and `ull`, but compiles them. At the same time, it highlights `f` but reports an error.... I wonder if this is a bug or a hint? :-)

 

If you are a beginner and are confused by our debate, let me explain that in C (and also C++) you can add suffixes to numeric literals that change the data type. For example, the suffix `u` turns an ordinary integer(int) into an unsigned integer(uint).

Try to execute a slightly modified script suggested by Vladimir Simakov (pay attention to the absence of spaces between numbers and letters, it is important):

void OnStart()
  {
//---
    
   Print(typename(1));
   Print(typename(-1));
   Print(typename(1 ll));
   
   Print(typename(1 u));
   Print(typename(1 ull));
   
   Print(typename(-1*1 ull));
   
   Print(typename(NULL));
   
   
   Print(-1<1 u); 
   Print(-1*1 ull);
  }

This script in the first lines outputs the type names of some literals that are compiled in MQL5.

I may not have compiled all working suffixes, I hope C specialists will correct me. Just try to understand the logic - based on the material of the article, the script output and our discussion, and if everything is not clear at all - ask questions.

Обсуждение статьи "Изучение MQL5 — от новичка до профи (Часть II): Базовые типы данных и использование переменных"
Обсуждение статьи "Изучение MQL5 — от новичка до профи (Часть II): Базовые типы данных и использование переменных"
  • 2024.06.11
  • Vladimir Simakov
  • www.mql5.com
Опубликована статья Изучение MQL5 — от новичка до профи (Часть II): Базовые типы данных и использование переменных : Автор: Oleh Fedorov...