Ignore:
Timestamp:
Apr 14, 2025, 9:54:29 AM (6 months ago)
Author:
Peter A. Buhr <pabuhr@…>
Branches:
master
Children:
10ef475
Parents:
d9aee90
Message:

update overload test programs for Ada and swift

Location:
doc/theses/fangren_yu_MMath
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • doc/theses/fangren_yu_MMath/test.adb

    rd9aee90 r68c7062  
    1616        Function Func( V1 : Integer; V2 : Float ) return Float is begin return Float(V1) + V2; end;
    1717       
    18         Function "-"( L, R : Integer ) return Integer is begin return L + (-R); end; --  prevent infinite recusion
     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
    1925       
    2026        i : Integer;
     
    3339           type T is private;
    3440           with function "+"( X, Y: T ) return T;
    35         function twice(X : T) return T;
     41        function twice( X : T ) return T;
    3642       
    3743        function twice( X: T ) return T is
     
    4349        function float_Twice is new Twice( float, "+" => "+" );
    4450       
    45         -- generic units cannot be overloaded
    46         -- generic
    47         --    type T is private;
    48         --    with function "+"( X, Y: T ) return T;
    49         -- function twice( X : T; Y : T ) return T;
    50         --
    51         -- function twice( X: T; Y : T ) return T is
    52         -- begin
    53         --    Put_Line( "XXX" ); return X + X;   -- The formal operator "*".
    54         -- end twice;
     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;
    5561begin
     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   
    5671        i := Random;
    5772        Print( i );
     
    6176        Print( s );
    6277       
    63         Print( Func( 7 ) );
     78        Print( Func( V => 7 ) );
    6479        Print( Func( 7.5 ) );
    6580        Print( Func( To_Unbounded_String( "abc" ) ) );
    6681        Print( Func( 3, 3.5 ) );
     82--      Print( Func( 3, 3 ) );
    6783       
    6884        Grind( X => (Re => 1.0, Im => 1.0) );
  • doc/theses/fangren_yu_MMath/test.swift

    rd9aee90 r68c7062  
    11// overloading on return type
    2 func random() -> Int{ 3; }
    3 func random() -> String{ "abc"; }
    4 func random() -> Double{ 3.5; }
     2func random() -> Int { 3; }
     3func random() -> String { "abc"; }
     4func random() -> Double { 3.5; }
    55var r1 : Int = random();
    66print( r1 );
     
    1010print( r3 );
    1111
    12 // overloading functions without parameter names
    13 func fun( _ x : Int ) -> Int{ 3; }
    14 func fun( _ x : Int, _ y : Int ) -> Int{ x + y; }
    15 func fun( _ x : String ) -> String{ "abc"; }
     12// function overloading without parameter names
     13//func fun( _ x : Int ) { 3; }
     14//func fun( _ x : Int, _ y : Int ) { 3; }
     15func fun( _ x : Double ) { 3; }
     16func fun( _ x : Int ) -> Int { 3; }
     17func fun( _ x : Int, _ y : Int ) -> Int { x + y; }
     18func fun( _ x : String ) -> String { "abc"; }
    1619print( fun( 3, 4 ) );
    1720// print( fun( 3.5 ) ); // no Double -> Int
     
    1922
    2023// overloading on parameter name
    21 func foo( x : Int ) -> Int{ 3; }
    22 func foo( y : Int ) -> Int{ 3; }
     24func foo( x : Int ) -> Int { 3; }
     25func foo( y : Int ) -> Int { 3; }
    2326print( foo( x : 3 ) );
    2427print( foo( y : 3 ) );
    2528
    26 // overloading on generics
     29// overloading on member name
     30class C {
     31    func foo() {}
     32    func foo( _ x: Int ) -> Int { 3; }
     33    func foo( _ x : Double, _ y : Double ) -> Double { x + y; }
     34    func foo( _ x : String ) -> String { "abc"; }
     35}
     36
     37let c = C()
     38c.foo();
     39r1 = c.foo( 3 );
     40r3 = c.foo( 3, 3.5 );
     41r2 = c.foo( "abc" );
     42
     43// generic overloading without parameter names
    2744func bar<T>( _ a : T ) { print( "bar1", a ); }
    2845func bar<T>( _ a : T, _ b : T ) { print( "bar2", a, b ); }
     
    3350bar( 2, 2.5 );
    3451
     52// generic overloading with parameter names
     53func baz( a : Int ) { print( "baz1", a ); }
     54func baz<T>( a : T ) { print( "baz1", a ); }
     55func baz<T>( a : T, b : T ) { print( "baz2", a, b ); }
     56func baz<T,U>( a : T, b : U ) { print( "baz3", a, b ); }
     57baz( a : 3 );
     58baz( a : 3.5 );
     59baz( a : 2, b : 2 );
     60baz( a : 2, b : 2.5 );
     61func swap<T>( _ a : inout T, _ b : inout T ) { let temp : T = a; a = b; b = temp; }
     62var ix = 4, iy = 3;
     63swap( &ix, &iy );
     64print( ix, iy );
     65var fx = 4.5, fy = 3.5;
     66swap( &fx, &fy );
     67print( fx, fy );
    3568// Local Variables: //
    3669// compile-command: "swift test.swift" //
Note: See TracChangeset for help on using the changeset viewer.