Nice TWiki > Doc > CodeExamples > RecursiveParametricTypeExample (r1.3) TWiki webs:
Dev | Doc | Main | TWiki | Sandbox
Doc . { Changes | Index | Search | Go }
/* Please try the NoviceExamples before the ParametricTypeExamples
To compile:
   nicec --sourcepath .. -a recursivetype.jar recursivetype
To run:
   java -jar recursivetype.jar
*/


interface IShape<T> {}

class Square<T> implements IShape { 
   double side; 
}

class Circle<T> implements IShape { 
   double radius; 
}

<IShape I, S, T | I<S> <: T <: I<S> >
class Translated<T> implements IShape { 
   Point d; 
   T shape; 
}

class Point { double x; double y; }
 


// Testing
//--------

void main(String[] args){
   let s = new Square(side: 4);
   let c = new Circle(radius: 2);

   let ts = 
      new Translated(
         d: new Point(x: 1.5, y: 0),  
         shape: s 
         );

   let tc = 
      new Translated(
         d: new Point(x: 1.5, y: 0),  
         shape: c 
         );

   println("Side of square = " + s.side);
   println("Side of translated square = " + ts.shape.side);

   println("Radius of circle = " + c.radius);
   println("Radius of translated circle = " + tc.shape.radius);        

   println("Side of translated translated square = " + 
     (new Translated(d: new Point(x: 4, y: 0), shape: ts)
        ).shape.shape.side ); 
}

/* Notes 

   Side of square = 4.0
   Side of translated square = 4.0
   Radius of circle = 2.0
   Radius of translated circle = 2.0
   Side of translated translated square = 4.0

*/
-- IsaacGouy? - 22 Apr 2004

Topic RecursiveParametricTypeExample . { Edit | Attach | Ref-By | Printable | Diffs | r1.5 | > | r1.4 | > | r1.3 | More }
Revision r1.3 - 23 Jan 2005 - 13:53 GMT - TWikiGuest
Parents: WebHome > CodeExamples
Copyright © 1999-2003 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TWiki? Send feedback.