Class Objects and Subtyping

    To all class objects are added methods deepClone, shallowEqual, etc in such a way all objects have a basic set of generic methods. Since these methods are also defined by class Any inherited by all classes, we have that:

Therefore all objects are subtypes of class Any. The type of an object is the set of signatures of its public methods. This flexible and simple type system is unique to Green.
 
 

    One method added to all class objects deserve a better explanation: method  cast for type conversion. An example of use of cast is shown below. Consider Circle subclass (or subtype) of Figure.

var f : Figure = Figure.new(...);
var c : Circle = Circle.new(...);
f = c;              // ok
c = Figure.cast(f); // ok
c = f;              // compile error: downcasting

    Method cast of object Figura has the following signature:

   proc cast( obj : Any )
            ( exception : CatchTypeErrorException ) : Figure

    Exception   CatchTypeErrorException will be thrown if the obj class at run time is not a subtype of Figure.
 
 

What is new ?

    The simplicity of this type system is one of the novelties of Green. Every object can be assigned to a variable whose type is Any because:

    This type system has (almost) all advantages of type systems in which all objects have a class and none of the problems associated to them. The use of classless objects to represent classes avoid the infinite regression problem: a class has a class (its metaclass) that has a class (meta metaclass) and so on.

Return