MQL equivalent to Python's None type? - page 3

 
Amir Yacoby:
This is why I said the std lib is not holy:

This is actually from the CArrayRing buffer class in the codebase, but I do agree that the stdlib isn't gospel. 


One final tweak to this in order to include object pointers in the case of setting NONE from templated methods/classes. 


class _NoneType{
 public:
   static double   None(double _)     { return DBL_MAX;   }
   static int      None(int _)        { return INT_MAX;   }
   static uint     None(uint _)       { return UINT_MAX;  }
   static long     None(long _)       { return LONG_MAX;  }
   static ulong    None(ulong _)      { return ULONG_MAX; }
   static char     None(char _)       { return CHAR_MAX;  }
   static uchar    None(uchar _)      { return UCHAR_MAX; }
   static short    None(short _)      { return SHORT_MAX; }
   static ushort   None(ushort _)     { return USHORT_MAX;}
   static string   None(string _)     { return "__NULL-*-*-STRING__";}
   static color    None(color _)      { return clrNONE;   }
   static datetime None(datetime _)   { return -1;        }
   template<typename T>
   static T        None(T _)          { return NULL;     }
};
#define NONE(T)  _NoneType::None((T)NULL)


void OnStart(){
   Print(get<int>());
   Print(get<double>());
   Print(get<string>());
   Print(get<datetime>());
   Print(get<CObject*>());
}

template <typename T>
T get(){ return NONE(T);}
 
nicholi shen:

This is actually from the CArrayRing buffer class in the codebase, but I do agree that the stdlib isn't gospel. 



I meant changing the CObject class.

 
Amir Yacoby:

I meant changing the CObject class.

Oh... I didn't see that you had added the methods to CObject. I'm not sure that I would agree with that because I'm sure there's a lot of descendant classes that already have defined a None method. Also, access to the methods would be limited to subclasses of CObject. This definitely belongs in the global scope so it can be accessed from anywhere. 

 
nicholi shen:

Oh... I didn't see that you had added the methods to CObject. I'm not sure that I would agree with that because I'm sure there's a lot of descendant classes that already have defined a None method. Also, access to the methods would be limited to subclasses of CObject. This definitely belongs in the global scope so it can be accessed from anywhere. 

Maybe I miss something, but if every object has the ability to tell it's decendents what is the None() value of every type they possibly can ask about, why do they need to access another object's None() methods?
 
nicholi shen:

descendant classes that already have defined a None method.

Call it PyNone to avoid clobbering ?

Just a thought . . .

 
Amir Yacoby:
Maybe I miss something, but if every object has the ability to tell it's decendents what is the None() value of every type they possibly can ask about, why do they need to access another object's None() methods?

Because this abstraction is not limited to CObject descendants. 

double g_value = NONE(double);

void OnTick()
{
   if(g_value == NONE(double) || Bid > g_value)
      g_value = Bid;
}
 
Anthony Garot:

Call it PyNone to avoid clobbering ?

Just a thought . . .

Of course.. And I don't even have None methods (search '::None(' in the global search in metaeditor).
 
Amir Yacoby:
And I don't even have None methods

Looks like I don't either.

 
nicholi shen:

Because this abstraction is not limited to CObject descendants. 

You mean if you write code outside of class? 
For me, it's not relevant. 

I am not going to define a dummy CObject just for calling those methods. 
It's for OO only.
 
Amir Yacoby:
You mean if you write code outside of class? 
For me, it's not relevant.

That's the entire point of this thread. No matter what scope, you should have access to a universal None type of which you can use to initialize any type in order to compare to the same None value. This, of course, is used to avoid collisions with ambiguous values. For example, 

string message = NULL;
...
message = "";

if(message == NULL) //true
   //ERROR... There's no way to check string for "None" value. (NULL == "")

string message = NONE(string);
...
message = "";

if(message == NONE(string)) //false
   //Correct way to check string for "None" value. (NONE(string) != "")
Reason: