I have a need to determine type of a variable which can then be used is a switch statement, and my thoughts are to hash the “typename” string of said var. Do you know a better way?
void OnStart()
{
int a; Print(type_id(a));
uint b; Print(type_id(b));
double c; Print(type_id(c));
CObject o; Print(type_id(o));
ENUM_TIMEFRAMES t; Print(type_id(t));
}
template <typename T>
int type_id(const T &var)
{
string tname = typename(var);
int len = StringLen(tname);
int hash = 0;
if(len > 0)
for(int i=0; i<len; i++)
hash = (31 * hash) + tname[i];
return hash;
}
Secondary question, since when are MQL strings subscriptable (can be accessed like arrays)???
string s = "abcdefg";
for(int i=0; i<StringLen(s); i++)
Print(CharToStr(s[i]));