You are missing trading opportunities:
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
Registration
Log in
You agree to website policy and terms of use
If you do not have an account, please register
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.
Could you rewrite this part of your code in your preferred language:
Could you rewrite this part of your code in your preferred language:
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)
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?
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)
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)
and CRTP also here...
anyway overriding will be needed in sub_classes from NumericVisitor & Visitable (or your class IFunction) -- and this is again
... always writing duplicate codes. // either overloading or overriding for each needed type or ifs or switch case ENUM
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