[4d5c5b6a] | 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 |
|
---|
[68c7062] | 18 | subtype Int is Integer;
|
---|
| 19 | J : Int;
|
---|
| 20 | Function "-"( L, R : Int ) return Int is begin Put_Line( "X" ); return Integer(L) + (-Integer(R)); end; -- prevent infinite recusion
|
---|
| 21 |
|
---|
| 22 | -- duplicate body for "-" declared at line 20
|
---|
| 23 | -- subtype SInt is Integer range -10 .. 10;
|
---|
| 24 | -- Function "-"( L, R : SInt ) return SInt is begin Put_Line( "X" ); return Integer(L) + (-Integer(R)); end; -- prevent infinite recusion
|
---|
[4d5c5b6a] | 25 |
|
---|
| 26 | i : Integer;
|
---|
| 27 | f : Float;
|
---|
| 28 | s : Unbounded_String;
|
---|
| 29 |
|
---|
| 30 | Type Complex is
|
---|
| 31 | record
|
---|
| 32 | Re, Im : Float;
|
---|
| 33 | end record;
|
---|
[f5bf3c2] | 34 | c : Complex := (Re => 1.0, Im => 1.0);
|
---|
[4d5c5b6a] | 35 | Procedure Grind (X : Complex) is begin Put_Line( "Grind1" ); end;
|
---|
| 36 | Procedure Grind (X : Unbounded_String) is begin Put_Line( "Grind2" ); end;
|
---|
| 37 |
|
---|
| 38 | generic
|
---|
| 39 | type T is private;
|
---|
| 40 | with function "+"( X, Y: T ) return T;
|
---|
[68c7062] | 41 | function twice( X : T ) return T;
|
---|
[4d5c5b6a] | 42 |
|
---|
| 43 | function twice( X: T ) return T is
|
---|
| 44 | begin
|
---|
| 45 | Put_Line( "XXX" ); return X + X; -- The formal operator "*".
|
---|
| 46 | end twice;
|
---|
| 47 |
|
---|
| 48 | function Int_Twice is new Twice( Integer, "+" => "+" );
|
---|
| 49 | function float_Twice is new Twice( float, "+" => "+" );
|
---|
[f5bf3c2] | 50 |
|
---|
[68c7062] | 51 | -- generic units cannot be overloaded
|
---|
| 52 | -- generic
|
---|
| 53 | -- type T is private;
|
---|
| 54 | -- with function "+"( X, Y: T ) return T;
|
---|
| 55 | -- function twice( X : T; Y : T ) return T;
|
---|
| 56 |
|
---|
| 57 | -- function twice( X: T; Y : T ) return T is
|
---|
| 58 | -- begin
|
---|
| 59 | -- Put_Line( "XXX" ); return X + X; -- The formal operator "*".
|
---|
| 60 | -- end twice;
|
---|
[4d5c5b6a] | 61 | begin
|
---|
[68c7062] | 62 | I := 3;
|
---|
| 63 | I := 7 - 3;
|
---|
| 64 | Print( i );
|
---|
| 65 | F := 3.8;
|
---|
| 66 | I := 7 - 2;
|
---|
| 67 | Print( i );
|
---|
| 68 | J := 7 - 3;
|
---|
| 69 | Print( j );
|
---|
| 70 |
|
---|
[4d5c5b6a] | 71 | i := Random;
|
---|
| 72 | Print( i );
|
---|
| 73 | f := Random;
|
---|
| 74 | Print( f );
|
---|
| 75 | s := Random;
|
---|
| 76 | Print( s );
|
---|
| 77 |
|
---|
[68c7062] | 78 | Print( Func( V => 7 ) );
|
---|
[4d5c5b6a] | 79 | Print( Func( 7.5 ) );
|
---|
| 80 | Print( Func( To_Unbounded_String( "abc" ) ) );
|
---|
| 81 | Print( Func( 3, 3.5 ) );
|
---|
[68c7062] | 82 | -- Print( Func( 3, 3 ) );
|
---|
[4d5c5b6a] | 83 |
|
---|
| 84 | Grind( X => (Re => 1.0, Im => 1.0) );
|
---|
| 85 | Grind( c );
|
---|
| 86 | Grind( To_Unbounded_String( "abc" ) );
|
---|
| 87 |
|
---|
| 88 | i := Int_Twice( 2 );
|
---|
| 89 | Put_Line( Integer'Image(i) );
|
---|
| 90 | f := float_Twice( 2.5 );
|
---|
| 91 | Print( f );
|
---|
| 92 | end test;
|
---|
| 93 |
|
---|
| 94 | -- Local Variables: --
|
---|
| 95 | -- tab-width: 4 --
|
---|
| 96 | -- compile-command: "gnatmake test.adb" --
|
---|
| 97 | -- End: --
|
---|