Can we overload functions on the basis of the return type of function only?
The answer is No for most programming languages. We cannot overload functions by using different return types for the functions. In other words, if there are at least two functions in the same context (e.g., the global context, class, struct or namespace) having the same data type of formal parameters and having exactly the same number of parameters (i.e., the same function signatures) then the compiler produces a compile-time error, due to failed function reolution by the compiler (ambiguity error).
The only programming language that I know of which allows function overloading on different return types (as the OP asks) is ADA. see here.
Can you consider to return an object wrapper, of specific derived class for each data type? Or return just a union, where only one field is filled and type is specified in a dedicated flag?
Of course, you can't use the returned entity in an expression right away - you need to unwrap the value first on the receiving end.
The whole point that he doesn't know the type of data in index 0 so no getData<string> is possible, and he still needs to assign it to a target var..
So it is impossible for the compiler to figure out which overload to call. the compiler can resolve overload by its function signature (function name + type + number + order of formal parameters). The return type is not part of the function signature.
Yasingh #: 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.
There is no possible generic solution to this issue in MQL currently. 5 years ago, I was facing similar issue (maybe not obvious it's similar but it is related to variant type)
What you would need is a variant "type", which is hard to implement in MQL.
If you can provide an example a bit less simplified, maybe the community can help, as the best solution depends on what you are really trying to achieve.
I was trying to use iCustom to dynamically load multiple indicators with varying parameters, aiming for a flexible and reusable setup. I thought maybe if I could make the idea more abstract my answer would not be as straightforward as this post. I know the reason now. However, the lack of a true variant type in MQL5 makes handling different outputs very challenging. It seems this limitation is inherent to the language.
Allow the use of cookies to log in to the MQL5.com website.
Please enable the necessary setting in your browser, otherwise you will not be able to log in.
You are signed in. Two-factor authentication has been disabled. Set it up again and scan a new QR code, or continue with two-factor authentication disabled.
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.
Can we overload functions on the basis of the return type of function only?
The answer is No for most programming languages. We cannot overload functions by using different return types for the functions. In other words, if there are at least two functions in the same context (e.g., the global context, class, struct or namespace) having the same data type of formal parameters and having exactly the same number of parameters (i.e., the same function signatures) then the compiler produces a compile-time error, due to failed function reolution by the compiler (ambiguity error).
The only programming language that I know of which allows function overloading on different return types (as the OP asks) is ADA. see here.
Can you consider to return an object wrapper, of specific derived class for each data type? Or return just a union, where only one field is filled and type is specified in a dedicated flag?
Of course, you can't use the returned entity in an expression right away - you need to unwrap the value first on the receiving end.
A partially working solution using template functions:
A partially working solution using template functions:
The whole point that he doesn't know the type of data in index 0 so no getData<string> is possible, and he still needs to assign it to a target var..
The whole point that he doesn't know the type of data in index 0 so no getData<string> is possible, and he still needs to assign it to a target var..
So it is impossible for the compiler to figure out which overload to call. the compiler can resolve overload by its function signature (function name + type + number + order of formal parameters). The return type is not part of the function signature.
Sorry for the lack of info. I thought it's better to be simplified. But overall you perfectly understood what I mean.
There is no possible generic solution to this issue in MQL currently. 5 years ago, I was facing similar issue (maybe not obvious it's similar but it is related to variant type)
What you would need is a variant "type", which is hard to implement in MQL.
If you can provide an example a bit less simplified, maybe the community can help, as the best solution depends on what you are really trying to achieve.
Another idea for isolating the functions (getData) with the same signatures into different contexts:
Thank you, everyone, for your responses!
I was trying to use iCustom to dynamically load multiple indicators with varying parameters, aiming for a flexible and reusable setup. I thought maybe if I could make the idea more abstract my answer would not be as straightforward as this post. I know the reason now. However, the lack of a true variant type in MQL5 makes handling different outputs very challenging. It seems this limitation is inherent to the language.
I appreciate all the help 🙏🙏🙏