OOP issue : idea to break a vicious circle ? - page 5

 
I think most people understand that MQL5 is not able to do that, they just look the best workaround..
 
Amir Yacoby #:
I think most people understand that MQL5 is not able to do that, they just look the best workaround..
I am afraid there is no satisfying workaround...it's always writing duplicate codes.
 
As there is no real generic way, the emphasis should be on what is most important. I thought that the need to not know upfront what is the type created and recieved. Its also about duplication. Anyway you probably found the most suitable way in the given limits and the needs of the project.
 
Alain Verleyen #:
I am afraid there is no satisfying workaround...it's always writing duplicate codes.

Could you rewrite this part of your code in your preferred language: 

//--- need to call functionA or functionB
   int originalIntValue       = NULL;
   double originalDoubleValue = NULL;

   if(ViciousCircle(values[0],originalIntValue))
     {
      functionA(originalIntValue);
     }
//--- Ok but what if know I want to call functionB if the original value is double ?
   else if(ViciousCircle(values[0],originalDoubleValue))
     {
      functionB(originalDoubleValue);
     }
// Imagine that for each basic type and for any functionX !!!
 
Laszlo Tormasi #:

Could you rewrite this part of your code in your preferred language: 

As I already said, it's an old topic. I don't have any need or time to invest on this for now. Maybe one day when I will be boring.
 
its discussions like this that make me all "warm and fuzzy" because after reading them, i dont feel as dumb anymore haha feels good to see that even the pro's have the same basic issues with mql-c++
 
Alain Verleyen   

Example, imagine I have to call a functionA with an int parameter, or a functionB with a double parameter.

of course if different functions you want to use for diff. types - you will need to define the type... but the idea of generalization is to make 1 function for diff. types (usually with template_fx)

Object Pointers - Data Types - Language Basics - MQL4 Reference
Object Pointers - Data Types - Language Basics - MQL4 Reference
  • docs.mql4.com
Object Pointers - Data Types - Language Basics - MQL4 Reference
 
Alain Verleyen #:
I am afraid there is no satisfying workaround...it's always writing duplicate codes.

I really do not clearly understand why (though also have no time to prove my intuition with code) - but theoretically I assume that we can try the 5th principle from SOLID (aka Dependency Inversion)... so we will not create the template class with certain functions for each Type, & thus typecasting needed... but

- 1st - of course we'd better use 2nd_principle (Interface Segregation) - smth like class ImyFunction (even template_class as for generalization) 

- 2nd - 5th principle (aka Dependency Inversion - logics like this) also can try... inheriting CMyObj from ImyFunction -- if such Decoupling really is needed (well, if this interface can be used for other classes in future or even just for independent development of it now)... in such a way making class MyObj less dependent on ImyFunction & even ImyFunction2 ... THOUGH (I'm not sure) multiple inheritance IS NOT possible in MQL4 ? [-- perhaps this is the reason of your convenience in unsufficient workaround] ... THEN probably in such a case we can inherit MyObj from ImyFunction (having overloaded Construtors & virtual fx in ImyFunction) & override fx in subclasses of ImyFunction... BUT in such a case we AGAIN will have ONE fx_name in diff. subclasses... and I suppose again typecasting needed?

use the explicit casting to convert the base class pointers to the pointers of a derived class

really, interesting theoretical (even without the code) architectural philosophic question about limitations & goals of encapsulation & generalization...

I still believe that any Decoupling can help...(but am also not having neither urgent example nor time to create artificial example for investigating & testing this question in MQL4 environment) ... and of course I still think we lack anonymous functions in MQL4... and  specifiers auto & decltype (like in c++), no try-catch

p.s.

though there is the example of Visitor_pattern - for interaction between 2 hierarchies (didn't test it yet)



Typecasting - Data Types - Language Basics - MQL4 Reference
Typecasting - Data Types - Language Basics - MQL4 Reference
  • docs.mql4.com
Typecasting - Data Types - Language Basics - MQL4 Reference
 
JeeyCi #:
    though there is the example of Visitor_pattern -

good example was in c++ here .. both Visit_pattern_logics (seems nothing that is prohibited in MQL now when template_classes appeared in the oct.2015)

struct NumericVisitor
{
    virtual void visit(double) = 0;
    virtual void visit(int) = 0;
    virtual void visit(unsigned char) = 0;
};

struct Visitable
{
    virtual void visitValue(NumericVisitor& visitor) = 0;
};

template<typename T>
class Derived : public Visitable
{
     T value;
public:
     Derived(const T& arg) value(arg) {}
     void visitValue(NumericVisitor& visitor) { visitor.visit(value); }
};

and CRTP  also here...

anyway overriding will be needed in sub_classes from NumericVisitor & Visitable (or your class IFunction) -- and this is again

Alain Verleyen #:
 ... always writing duplicate codes.   // either overloading or overriding for each needed type or ifs or switch case ENUM
nevertheless, it is useful Decoupling here - for the sake of generalization (though not for templatation only)
Array of template objects
Array of template objects
  • 2011.04.30
  • Max Well Max Well 41 1 1 silver badge 2 2 bronze badges
  • stackoverflow.com
I don't know how to resolve a problem with templates and inheritance. In my code there is a templated class which looks more or less like: The only purpose of my Base class is to group an array of objects of the Derived class. The parameter T is generally double, float or complex, although int and structs might also become useful. (There should...
 

well. at the end I propose:

to forget about patterns as so as any Abstract classes (BTW they seems to appear in 2016_yr) & Inheritance are out of easiness to maintain in the project (I'm not sure that run-time in MQL could not be fully substituted with compile-time-only type identification with means of appropriate Architecture, -- plus the pointer in MQL is really not a memory block (sizeof()=4 or etc. depending on the compiler) in run-time of MQL, but just a handler to certain type - imho)...

of course to avoid RTTI...

and concerning templates - try to do any template_specialization for certain types if needed or of course overloading can also help but for functions with one name (& even both template_function & member_function with one name can exist in one class - at least in c++)...

besides, not to create container filling with pointers to different datatyped objects  ...

if putting to the container pointers then, looping it, first get the element it is pointing to & only then assign to certain function (overloaded) -- should work (if problems with pointers exist)...

though of course overloading is still somewhat duplication of code as opposed to the generalization of templates...

(am going to be ruled with such a schema while not meeting the problem of the author) - yet see no reason to make any difficulties in my code

wish clear developing for everybody & do not forget about Decoupling anyway - to weaken dependencies 

Reason: