Is there any way to declare a function that can take both an int and a double as it's argument in MQL4?

532 views Asked by At

I have these two functions:

void CheckOneConditionInt( int    &SettingsEditValueInt );
void CheckOneConditionDbl( double &SettingsEditValueDbl );

They do the same stuff, but one is used with int values and another is used with double values. Is there any way to make it one function that can take int/double as an argument? For example:

void CheckOneCondition( void &SettingsEditValue );

PS: The example does not work of course.

3

There are 3 answers

5
Enivid On BEST ANSWER

The problem is solved by using templates:

template<typename T>
void CheckOneCondition( T &SettingsEditValue );

I can then call it passing double or int parameter.

2
user3666197 On

Yes and No.

While the New-MQL4.56789 extensions have introduced classes ( inside which there are means for multiple calling interfaces ( where one could being suited for double and another for int ), which could mean YES ),
the MQL4 is in its initial design principles a compiled language with strong typing,
which means NO.


Design decision rules

A sound and clear desing can answer the dilemma. There is no principal reason to have ambiguous, dual-typed calling interface in MQL4 problem domain.

Except for some theoretical experimentation, there is always a deterministic certainty, of what type the value to be passed to a function is, thus an amorphous dual-(multi)-typed calling interface is moreless an academical subject in MQL4 context of use.


Epilogue

If one indeed seeks for such an extremely artificial geekiness to achieve such non-deterministic agnosticism, let's first define a clear MQL4-domain use-case, for which such uncertaintiness of parameter type is both necessary and un-avoidable by other, available, means.

4
Daniel Kniaz On

Have you heard about method overloading? It is used in MQL4.5. So, if you pass int value then Function( int value ) is called, if real - then Function( double value ) is called. If the algorithm applied to both types is same - maybe you can just skip Function( int value ), or add a wrapper inside the integer function, something like:

Function( int &value ){
   double tmp = value/1.0;       
   Function( tmp );
   value = ( int ) Normalize( tmp, 0 );
}