Saturday, July 29, 2006

Mynx 'in' operator or .isKindOf like Java's instanceof

One design consideration in Mynx is for an instanceof for Java, or is for C#, an operator or method that checks whether an instance of an object is a type or kind of class. But, Java' s instanceof operator checks only if an object is an instance of a class...such as:

Java:

if(myObject instanceof String)
{

}


C#:

if(myObject is String)
{

}


but in Mynx the operator 'in' determines like instanceof, but also allows object instance to object instance, like:


if(myObject in String) //check if myObject instance of a String class

end if;


or


if(myObject in obj) //check if myObject same reference/pointer to obj

end if


The design question is to allow the 'in' operator to check among instance objects are type-class compatible, and if it is more efficient to simply use a method instead of an operator. This is more flexible than Java's instanceof operator, but through the object hierarchy can create a method that is inherited like "isEqual" but "isKindOf" method. The virtual class (Mynx has no interfaces) VObject would have the form:


virtual class VObject as Void is

     Bool isEqual(VObject);
     Bool isKindOf(VObject);
 
     overload constant == as isEqual;

end class;



The major difference between "isEqual" and "isKindOf" is that equality is the same object reference or two objects of the same class in the same state, whereas kind of means objects are type-compatible or have a common ancestor or super-class in the class hierarchy. Also, the equality check "==" operator is overloadable, whereas the "in" operator cannot be overloaded.

Using an operator has the advantage of syntactic neatness...something Java's syntax in comparison to say C++ or C# is no match in expressivity because of lack of operator overloading. The drawback does not apply to the "in" operator -- determining at compile-time which operator is associated with a method of the class and translating for the method call.

Labels: , ,

Website Spy Software