Is it possible to make functions with a variable number of args in MQL?

To add comments, please log in or register
nicholi shen
1869
nicholi shen  

I want to make something similar to Print(arg1, arg2, ...)

Is it possible or do I need to make n overloaded functions to essentially accomplish the same thing?

Carl Schreiber
Moderator
7068
Carl Schreiber  

Nope!

But you can 'overload' functions like:

double myFunc(double arg1) {..}

double myFunc(double arg1, double arg2) {..}

double myFunc(double arg1,double arg2, double arg3) {..}

Stanislav Korotky
24522
Stanislav Korotky  
nicholishen:

I want to make something similar to Print(arg1, arg2, ...)

Is it possible or do I need to make n overloaded functions to essentially accomplish the same thing?

You can make arguments optional - for example, Method(type1 name1 = 0, type2 name2 = 0), then call Method(), Method(var1) or Method(var1, var2).

whroeder1
17945
whroeder1  
nicholishen: I want to make something similar to Print(arg1, arg2, ...) Is it possible or do I need to make n overloaded functions to essentially accomplish the same thing?

Can't do that in general.

You can make arguments optional. On the right with the "= Constant" and on the left with overloading (See Download history in MQL4 EA - MQL4 and MetaTrader 4 - MQL4 programming forum.)

nicholi shen
1869
nicholi shen  
whroeder1:

Can't do that in general.

You can make arguments optional. On the right with the "= Constant" and on the left with overloading (See Download history in MQL4 EA - MQL4 and MetaTrader 4 - MQL4 programming forum.)


In general, almost all modern languages have a way to implement it including c++, but I assume you are talking about MQL? 


I wound up using cascading functions and method chaining. 


Example:

class MyClass
{   
public:
   template<typename T1>
   MyClass*    MyFunc(T1 var1)
   {
      // do something
      return &this;
   }
   
   template<typename T1,typename T2>
   MyClass*    MyFunc(T1 var1,T2 var2)
   {
      return MyFunc(var1).MyFunc(var2);
   }
   
   template<typename T1,typename T2,typename T3>
   MyClass*     MyFunc(T1 var1,T2 var2,T3 var3)
   {
      return MyFunc(var1,var2).MyFunc(var3);
   }
   // ..... etc 
}
To add comments, please log in or register