| 1 | with Ada.Text_IO; use Ada.Text_IO;
|
|---|
| 2 | with Ada.Strings.Unbounded; use Ada.Strings.Unbounded;
|
|---|
| 3 |
|
|---|
| 4 | procedure test is
|
|---|
| 5 | Function Random return Integer is begin return 3; end;
|
|---|
| 6 | Function Random return Float is begin return 3.5; end;
|
|---|
| 7 | Function Random return Unbounded_String is begin return To_Unbounded_String( "abc" ); end;
|
|---|
| 8 |
|
|---|
| 9 | Procedure Print( V : Integer ) is begin Put_Line( Integer'Image(V) ); end;
|
|---|
| 10 | Procedure Print( V : Float ) is begin Put_Line( Float'Image(V) ); end;
|
|---|
| 11 | Procedure Print( V : Unbounded_String ) is begin Put_Line( Ada.Strings.Unbounded.To_String(V) ); end;
|
|---|
| 12 |
|
|---|
| 13 | Function Func( V : Integer ) return Integer is begin return V; end;
|
|---|
| 14 | Function Func( V : Float ) return Float is begin return V; end;
|
|---|
| 15 | Function Func( V : Unbounded_String ) return Unbounded_String is begin return V; end;
|
|---|
| 16 | Function Func( V1 : Integer; V2 : Float ) return Float is begin return Float(V1) + V2; end;
|
|---|
| 17 |
|
|---|
| 18 | Function "-"( L : Integer; R : Integer ) return Integer is begin return 3; end;
|
|---|
| 19 |
|
|---|
| 20 | i : Integer;
|
|---|
| 21 | f : Float;
|
|---|
| 22 | s : Unbounded_String;
|
|---|
| 23 |
|
|---|
| 24 | Type Complex is
|
|---|
| 25 | record
|
|---|
| 26 | Re, Im : Float;
|
|---|
| 27 | end record;
|
|---|
| 28 | Procedure Grind (X : Complex) is begin Put_Line( "Grind1" ); end;
|
|---|
| 29 | Procedure Grind (X : Unbounded_String) is begin Put_Line( "Grind2" ); end;
|
|---|
| 30 | c : Complex;
|
|---|
| 31 |
|
|---|
| 32 | generic
|
|---|
| 33 | type T is private;
|
|---|
| 34 | with function "+"( X, Y: T ) return T;
|
|---|
| 35 | function twice(X : T) return T;
|
|---|
| 36 |
|
|---|
| 37 | function twice( X: T ) return T is
|
|---|
| 38 | begin
|
|---|
| 39 | Put_Line( "XXX" ); return X + X; -- The formal operator "*".
|
|---|
| 40 | end twice;
|
|---|
| 41 |
|
|---|
| 42 | generic
|
|---|
| 43 | type T is private;
|
|---|
| 44 | with function "+"( X, Y: T ) return T;
|
|---|
| 45 | function twice( X : T; Y : T ) return T;
|
|---|
| 46 |
|
|---|
| 47 | -- generic units cannot be overloaded
|
|---|
| 48 | function twice( X: T; Y : T ) return T is
|
|---|
| 49 | begin
|
|---|
| 50 | Put_Line( "XXX" ); return X + X; -- The formal operator "*".
|
|---|
| 51 | end twice;
|
|---|
| 52 |
|
|---|
| 53 | function Int_Twice is new Twice( Integer, "+" => "+" );
|
|---|
| 54 | function float_Twice is new Twice( float, "+" => "+" );
|
|---|
| 55 | begin
|
|---|
| 56 | i := Random;
|
|---|
| 57 | Print( i );
|
|---|
| 58 | f := Random;
|
|---|
| 59 | Print( f );
|
|---|
| 60 | s := Random;
|
|---|
| 61 | Print( s );
|
|---|
| 62 |
|
|---|
| 63 | Print( Func( 7 ) );
|
|---|
| 64 | Print( Func( 7.5 ) );
|
|---|
| 65 | Print( Func( To_Unbounded_String( "abc" ) ) );
|
|---|
| 66 | Print( Func( 3, 3.5 ) );
|
|---|
| 67 |
|
|---|
| 68 | Grind( X => (Re => 1.0, Im => 1.0) );
|
|---|
| 69 | Grind( c );
|
|---|
| 70 | Grind( To_Unbounded_String( "abc" ) );
|
|---|
| 71 |
|
|---|
| 72 | i := Int_Twice( 2 );
|
|---|
| 73 | Put_Line( Integer'Image(i) );
|
|---|
| 74 | f := float_Twice( 2.5 );
|
|---|
| 75 | Print( f );
|
|---|
| 76 | end test;
|
|---|
| 77 |
|
|---|
| 78 | -- Local Variables: --
|
|---|
| 79 | -- tab-width: 4 --
|
|---|
| 80 | -- compile-command: "gnatmake test.adb" --
|
|---|
| 81 | -- End: --
|
|---|