OOP, templates and macros in mql5, subtleties and uses - page 5

 
pavlick_:

About multi-passing - rushed, naively thought the mcl would allow it

So there are variables here. And with functions - yes, it is multi-passable, so everything was right. The problem arises precisely because of the order of initialization of functions. In short, in C++ there is one strict order relating to variables, functions and types. And in MQL it's all different.
 
Alexey Navoykov:
Ну вот я с этого и начинал разговор тут. Планировал тоже заменять все статики на глобалы (хоть это и жесть конечно).  Но как показано выше, с шаблонами такое не прокатит.  С макросами тоже.  А я всё это широко применяю.  Поэтому и сделал свою реализацию.  Хотя она конечно не решает всех проблем.  Динамические массивы по-прежнему нельзя инициализировать, константные типы тоже.  Поэтому их однозначно придётся выносить на глобальный уровень

I also use templates and macros extensively, but consider free functions only as auxiliary (and generally undesirable) architectural elements. When all implementations are packed inside objects and statics are declared only at class level, annoying bugs appear except that sometimes it's hard to understand the logic of their correct sequence of declarations, so that the compiler doesn't swear...

 
Alexey Navoykov:
So there are variables here. And with functions, yes, it is multi-passable, so everything was right. The problem arises precisely because of the order of initialization of functions. In short, in C++ there is one strict order that applies to variables, functions and types. And in MQL everything is different.

Multiple passes are bad in any case - you may encounter recursion, deadlocks etc. But on the plus side it can be done. Therefore only wisely, cautiously (i.e. forward declaration), and not as now with multi-pass compiler - as if for any function forward declaration is made, sooner or later this rake will hit your forehead.

 
pavlick_:

Multipassing is bad in any case - you can run into recursion, deadlock, etc. But in pluses it is possible to do forward declaration. Therefore only wisely, carefully, and not as now with multipass compiler - as if for any function forward declaration is made, sooner or later this rake will hit your forehead.

I agree. This topic was discussed a bit earlier. It's what many here like about the language - that you don't bother with the order of functions declaration. Needless to say, I was happy about it myself some time ago). But its tediousness irritated me on the plus side. But with experience comes understanding.

 
Ilya Malev:

I also use templates and macros extensively, but consider free functions only as auxiliary (and generally undesirable) architectural elements. When all implementations are packed inside objects and statics are declared only at class level, annoying bugs appear except that sometimes it's hard to understand the logic of their correct sequence, so that the compiler won't swear...

I agree, if everything is clearly lined up in OOP then not only free functions are not needed, but generic methods too, but here we are dealing with an underdeveloped hybrid between C++ and C#, that's why we have to implement a lot of things with crutches )
 
Alexey Navoykov:
I agree, if everything is clearly lined up in OOP, then not only free functions are not needed, but template methods too.

Template methods are needed wherever you don't want to write a bunch of repetitive code, either OOP or non-ODOP. Free functions are another matter - these are different programming styles.

 
Ilya Malev:

Template methods are needed wherever you don't want to write a bunch of repetitive code, whether it's OOP or non-ODOP

In OOP, interfaces are designed to do this.
 
Alexey Navoykov:
In OOP, interfaces were invented for this purpose.

Interfaces are a bit different. If I want the same code to do the same job with different types depending on the parameter class (without declaring additional classes), interfaces won't help.

 
Ilya Malev:

Interfaces are a bit different. If I want the same code to do the same job with different types depending on the parameter class (without declaring additional classes), the interface won't help.

If the parameters are of different types, then it makes sense to make several overloaded methods with corresponding types. You'll still have to separate them somehow in the function. So it's better to divide them into separate functions, than to create a mess that also takes an impersonal type, i.e. by mistake you can pass anything into it and get a compilation error inside the library, which is not good. And maybe even not, which is doubly bad).

In short, in full-fledged OOP template functions are a crutch (with few exceptions)

 
Alexey Navoykov:


In short, template functions are a crutch in full-fledged OOP.

So here we are.

Forum on trading, automated trading systems and trading strategies testing

Features of mql5 language, tips and tricks

Alexey Navoykov, 2019.01.25 10:11

Well, that's how I started the conversation here. I was planning to replace all statics with globals too (although it's a cruel thing, of course). But as shown above, it won't work with templates and macros too. And I use them all widely. That's why I made my own implementation. Although it doesn't solve all the problems. Dynamic arrays still can't be initialized, neither can the constant types. That's why they will definitely have to be globalized.
So it turns out that all your code is made on crutches? It's nothing personal.
Reason: