Parameterized Classes

    A parameterized class Stack may be declared in Green as

 

class Stack(T : Any)
   ...

   public:

     proc push( x : T ) ...

     proc pop() : T ...

     ...

   private:  

     var v : array(T)[];

         size : integer;

end

in which T must be used inside Stack as a type. This class may be used as in

var s : Stack(Person);

s.push( Person.new(“Mary”, 30) );

var p : Person;

p = s.pop();

...

The compiler creates a new class when it finds Stack(Person). Parameter T of class Stack is replaced by Person and this new class is compiled.  Had Stack been declared as

class Stack(T : Window)
   ...

end

 

then the real parameters to Stack could only be Window and its subtypes. And the class that is parameter should define all constructors Window defines. That is, if Window defines

   proc init()

   proc init( x1, y1, x2, y2 : integer)

so should a real parameter Icon to Stack:

   var si : Stack(Icon);