Functions or Methods with the same name and inputs but different types of outputs

 
Hello Everyone
is there in any way possible to create something that could make this possible?
to have a function or method? a structure, class, some data type or anything that could solve this problem?
string getData(int index)
  {
   return "string";
  }
int getData(int index)
  {
   return 5;
  }
double getData(int index)
  {
   return 5.0;
  }
bool getData(int index)
  {
   return false;
  }
this is super simplified and of course does not work for many reasons but exactly what i need. 
Thank You in advance 🙏🙏🙏 
 

You can do function overloading.

But what you want can easily be achieved in other ways, Type casting:

int getData(int index)
  {
   return 5;
  }
...
string str = (string)getData(index);
int i = (int)getData(index);
double d = (double)getData(index);
Documentation on MQL5: Language Basics / Functions / Function Overloading
Documentation on MQL5: Language Basics / Functions / Function Overloading
  • www.mql5.com
Usually the function name tends to reflect its main purpose. As a rule, readable programs contain various well selected identifiers . Sometimes...
 
Carl Schreiber #:

You can do function overloading.

But what you want can easily be achieved in other ways, Type casting:

Carl Schreiber #:

You can do function overloading.

But what you want can easily be achieved in other ways, Type casting:

Thank you 
But function overloading needs different inputs to work. the same function with different output results in error.
And type casting is not suitable for my use I need to completely find the type of the output internally.
And then return something based on that.

 

Functions/methods with the exact name and parameter types cannot exit into the same context.

I am thinking of using namespaces to isolate the contexts:

namespace ns_str {
string getData(int index)
  {
   return "string";
  }
};

namespace ns_int {
int getData(int index)
  {
   return 5;
  }
};

namespace ns_dbl {
double getData(int index)
  {
   return 5.0;
  }
};

namespace ns_bool {
bool getData(int index)
  {
   return false;
  }
};

void OnStart()
  {
   Print( ns_str::getData(0)  );
   Print( ns_int::getData(0)  );
   Print( ns_dbl::getData(0)  );
   Print( ns_bool::getData(0) );
  }
 
amrali #:

Functions/methods with the exact name and parameter types cannot exit into the same context.

I am thinking of using namespaces to isolate the contexts:

Beautiful approach 
Still while I'm running getData I have no idea what is the type of the output and inside is where I want to figure it out. 
Interesting approach though.
Thanks 🙏🏻 🙏🏻 🙏🏻 
 
Do you need it in the form of assignment, or can it be assigned to an output parameter?
 
Amir Yacoby #:
Do you need it in the form of assignment, or can it be assigned to an output parameter?
By assignment it is.
 If I could use the output parameter it would be amazing because I could use a template function. 
Unfortunately I need it by assignment.
 
Yasingh #:
By assignment it is.
 If I could use the output parameter it would be amazing because I could use a template function. 
Unfortunately I need it by assignment.

And mixing them is not acceptable?

template<typename T>
T GetData(int index,T parm)
 
Amir Yacoby #:

And mixing them is not acceptable?

Not really as you see for using template functions like this you have to add it in input. Which is not ok for my thing.
Thanks though 🙏🏻🙏🏻🙏🏻
 
Yasingh #:
Not really as you see for using template functions like this you have to add it in input. Which is not ok for my thing.
Thanks though 🙏🏻🙏🏻🙏🏻

You have to find an alternative because there is no way to determine the receiving type by the sending type.
And for a satisfiable alternative, you need to supply more info to the use case, something more than my thing.



 
Amir Yacoby #:

You have to find an alternative because there is no way to determine the receiving type by the sending type.
And for a satisfiable alternative, you need to supply more info to the use case, something more than my thing.



Sorry for the lack of info. I thought it's better to be simplified. But overall you perfectly understood what I mean.
This task seems completely impossible, due to the static nature of the language but I thought maybe there is something new out there that I could learn.