Classes as Objects


    Classes are objects in Green. The code

     class Figure
       public:
         proc init( x : integer; y : integer )
         proc draw()
         ...
     end

is a class declaration. This class is used to create objects of Figure. The object representing class Figure is declared as
     object Figure
       public:
         proc getMaxWidthFigure() : integer
           ...
       ...
     end
and will be called "class object Figure". This example defines an object as in delegation-based languages in which classes do not exist.

    The constructor of a class is always called init. For each method init of a class the compiler creates a new method in the class object that calls the class init method. So, class object Figure has a method
     new( x : integer; y : integer )
Therefore, one can write
     var f : Figure;
     f = Figure.new( 5, 20 );
 
 

What is new ?

    These two items make Green much simpler than languages with similar power. Since a class object does not have a class, the infinite regression problem does not exist. This problem is stated as "a class has a metaclass (its class), the metaclass has a meta-metaclass, and so on".

Return