Nice TWiki > Doc > CodeExamples > NumberFiveParametricTypeExample (r1.2) TWiki webs:
Dev | Doc | Main | TWiki | Sandbox
Doc . { Changes | Index | Search | Go }
/* Please try the NoviceExamples before the BeginnerExamples
To compile:
   nicec --sourcepath=.. -a numberfive.jar numberfive
*/

< T | T <: double, int <: T > T numberFive(){ return 5; }

void main(String[] args){  
   int a = numberFive(); 
   println( a.getClass.getName + " " + a);

   long b = numberFive(); 
   println( b.getClass.getName + " " + b);

   float c = numberFive(); 
   println( c.getClass.getName + " " + c);

   double d = numberFive(); 
   println( d.getClass.getName + " " + d + '\n');

   let e = numberFive(); 
   println( e.getClass.getName + " " + e);
}  


/* Notes - language
   < T | T <: double, int <: T > 
Any type T, where T is a subtype of primitive type double
and T is type int or above.

    T numberFive(){ return 5; }
numberFive() returns a value of type T

   Nice\test>java -jar t.jar
   java.lang.Integer 5
   java.lang.Long 5
   java.lang.Float 5.0
   java.lang.Double 5.0

   java.lang.Integer 5
*/


/* Please try the NoviceExamples before the BeginnerExamples
To compile:
   nicec --sourcepath=.. -a numberfive.jar numberfive
*/

< T | T <: double, int <: T > T numberFive(){ return long(5); }

void main(String[] args){  
   int a = numberFive(); 
   println( a.getClass.getName + " " + a);

   long b = numberFive(); 
   println( b.getClass.getName + " " + b);

   float c = numberFive(); 
   println( c.getClass.getName + " " + c);

   double d = numberFive(); 
   println( d.getClass.getName + " " + d + '\n');

   let e = numberFive(); 
   println( e.getClass.getName + " " + e);
}  


/* Notes - language
   < T | T <: double, int <: T > 
Any type T, where T is a subtype of primitive type double
and T is type int or above.

   T numberFive(){ return long(5); }
Cast 5 to primitive type long

   Nice\test\..\test\test.nice: line 6, column 47:
   Incorrect return type:
   Found   : nice.lang.long
   Expected: T

T the return type of numberFive is defined to include primitive
type int - and long to int coercion is required to be explicit 
because it may result in data loss - so, because T includes type 
int, long(5) does not match the return type T. 
*/


/* Please try the NoviceExamples before the BeginnerExamples
To compile:
   nicec --sourcepath=.. -a numberfive.jar numberfive
*/

< T | T <: double, long <: T > T numberFive(){ return long(5); }

void main(String[] args){  
   long b = numberFive(); 
   println( b.getClass.getName + " " + b);

   float c = numberFive(); 
   println( c.getClass.getName + " " + c);

   double d = numberFive(); 
   println( d.getClass.getName + " " + d + '\n');

   let e = numberFive(); 
   println( e.getClass.getName + " " + e);
}  


/* Notes - language
   < T | T <: double, long <: T > 
Any type T, where T is a subtype of primitive type double
and T is type long or above.

   T numberFive(){ return long(5); }
Cast 5 to primitive type long

   Nice\test>java -jar t.jar
   java.lang.Long 5
   java.lang.Float 5.0
   java.lang.Double 5.0

   java.lang.Long 5
*/

-- IsaacGouy - 11 Oct 2003

Topic NumberFiveParametricTypeExample . { Edit | Attach | Ref-By | Printable | Diffs | r1.8 | > | r1.7 | > | r1.6 | More }
Revision r1.2 - 11 Oct 2003 - 19:36 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.