Nice TWiki > Doc > CodeExamples > GenericProgrammingBeginnerExample TWiki webs:
Dev | Doc | Main | TWiki | Sandbox
Doc . { Changes | Index | Search | Go }
/* Please try the NoviceExamples before the BeginnerExamples

To compile:   
   nicec --sourcepath .. -d . -a generic.jar generic

To run:
   java -jar generic.jar 
*/

abstract interface Comparable {                  
   boolean better(alike); 
}

<Comparable T> T pick(T a, T b){
   if (a.better(b)) return a; else return b;    
}

class Apple implements Comparable {  
   better(Apple other) = other.rating < rating; 

   int rating;
}

class Orange implements Comparable {  
   better(Orange other) = other.name.compareTo(name) < 0; 

   String name;
}


void main(String[] args){
   let a1 = new Apple(rating: 3);
   let a2 = new Apple(rating: 5);
   let a3 = pick(a1,a2);
   println( a3 + "   " +  a3.rating );

   let o1 = new Orange(name: "Miller");
   let o2 = new Orange(name: "Portokalos");
   let o3 = pick(o1,o2);
   println( o3 + "   " +  o3.name );
}

/* Notes - language
Compare with C++, ML, Haskell, Eiffel, Java Generics, Generic C# versions in 
http://www.osl.iu.edu/publications/pubs/2003/comparing_generic_programming03.pdf
*/

-- IsaacGouy - 28 Dec 2003

Topic GenericProgrammingBeginnerExample . { Edit | Attach | Ref-By | Printable | Diffs | r1.2 | > | r1.1 | More }
Revision r1.2 - 10 Jan 2004 - 15:35 GMT - IsaacGouy
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.