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
How about processing the generic value with a function array.
And yes, you could build a 'custom' union to hold more complex types.Yes I thought about the function pointer usage but didn't explore it yet.
I will keep you posted later, thanks for all.
https://www.mql5.com/en/code/19314
Not perfect but maybe this can help.
I updated because the template is available in struct, as opposed to what I've said earlier.
Not perfect but maybe this can help.
There is no template for structures - It compiles but not run. So, I don't use the Set method, just left it there in case you might be interested to wrap it in a class.
From original post :
So first question : is there a better solution than a factory method and an ENUM ?
1. The data comes from a text file, they are string, it's a requirement. So you can't do that :
Example, imagine I have to call a functionA with an int parameter, or a functionB with a double parameter.
I will need to declare a variable for any type, then call the "dynamic cast" function, trying one by one :
2. Your code does the same as the code I posted, the details are just hidden in a dictionary. Only difference, when you need to use the values you are using a "typename", which is just similar to "switch" or "if". You still need to enumerate all types, which is what I am trying to avoid.
3. @lippmaje understood well the problem, so I guess what I want is correctly articulated. Feel free to ask question about what is not clear for you.
4. Please stop your sarcasms and your provocative attitude, if you can't, do me a favor : ignore me.
Hello,
So, if I understand it correctly you have the file with entries like "INT:1234" and "DOUBLE:3.15" and so on and you do want to create a vector with them - i.e. an array compromised of various types. Then, you are saying you do want to know what type sits in each vector position so yo9u can use it in a e.g. proper function. So, some remarks from me:
a) Why creating a vector in the first place and not create instead all the wimple arrays you want ? Maybe this is "circle" you are referring to, maybe you do not want that compound vector
b) OK, let 's say you do need that compound vector (which by the way will be slower than simple arrays), but next you have to know the type on each position ; first of all, I am not sure MQL has that knowledge either; MetaQuotes could very well only store the necessary information concerning the size on each entry on the vector - and that only to use it for IO operations on that vector anyway; so it is maybe up to us to keep a record of what is going on in the vector. So I would propose to create a simple array along with the vector and each time you create an entry in the vector create a corresponding entry in the array which would tell the kind of type that exists in the vector - normally an array with an enum type - for visibility
greets
Hello,
So, if I understand it correctly you have the file with entries like "INT:1234" and "DOUBLE:3.15" and so on and you do want to create a vector with them - i.e. an array compromised of various types. Then, you are saying you do want to know what type sits in each vector position so yo9u can use it in a e.g. proper function. So, some remarks from me:
a) Why creating a vector in the first place and not create instead all the wimple arrays you want ? Maybe this is "circle" you are referring to, maybe you do not want that compound vector
b) OK, let 's say you do need that compound vector (which by the way will be slower than simple arrays), but next you have to know the type on each position ; first of all, I am not sure MQL has that knowledge either; MetaQuotes could very well only store the necessary information concerning the size on each entry on the vector - and that only to use it for IO operations on that vector anyway; so it is maybe up to us to keep a record of what is going on in the vector. So I would propose to create a simple array along with the vector and each time you create an entry in the vector create a corresponding entry in the array which would tell the kind of type that exists in the vector - normally an array with an enum type - for visibility
greets
The circle I am referring to is the following, if you want to create generic code (in Stepanov sense), you use templates. Then if you want to create dynamic and flexible code (so a code where the minimum "stuff" are hard coded and fixed at compile time, but rather at run time), you will use polymorphism. When you try to combine that you have "problems", templates are static and limit the "polymorphism" of your code.
I could use typed arrays but that's at the opposite of my purpose, and is just moving the problem somewhere else. I talked about vectors, someone proposed dictionary, you arrays, it's all fine.
The problem with your proposal to use an array beside the vector is an other way similar to what I use, you will still have the problem to match your ENUM and mql types. So enumerate and repeat code. Probably what I want to achieve is not doable in mql and I will have to make some compromises.
Thanks for your input.