Changeset 68c7062 for doc/theses/fangren_yu_MMath
- Timestamp:
- Apr 14, 2025, 9:54:29 AM (6 months ago)
- Branches:
- master
- Children:
- 10ef475
- Parents:
- d9aee90
- Location:
- doc/theses/fangren_yu_MMath
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
doc/theses/fangren_yu_MMath/test.adb
rd9aee90 r68c7062 16 16 Function Func( V1 : Integer; V2 : Float ) return Float is begin return Float(V1) + V2; end; 17 17 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 19 25 20 26 i : Integer; … … 33 39 type T is private; 34 40 with function "+"( X, Y: T ) return T; 35 function twice( X : T) return T;41 function twice( X : T ) return T; 36 42 37 43 function twice( X: T ) return T is … … 43 49 function float_Twice is new Twice( float, "+" => "+" ); 44 50 45 --generic units cannot be overloaded46 --generic47 --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 is52 --begin53 --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; 55 61 begin 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 56 71 i := Random; 57 72 Print( i ); … … 61 76 Print( s ); 62 77 63 Print( Func( 7 ) );78 Print( Func( V => 7 ) ); 64 79 Print( Func( 7.5 ) ); 65 80 Print( Func( To_Unbounded_String( "abc" ) ) ); 66 81 Print( Func( 3, 3.5 ) ); 82 -- Print( Func( 3, 3 ) ); 67 83 68 84 Grind( X => (Re => 1.0, Im => 1.0) ); -
doc/theses/fangren_yu_MMath/test.swift
rd9aee90 r68c7062 1 1 // overloading on return type 2 func random() -> Int { 3; }3 func random() -> String { "abc"; }4 func random() -> Double { 3.5; }2 func random() -> Int { 3; } 3 func random() -> String { "abc"; } 4 func random() -> Double { 3.5; } 5 5 var r1 : Int = random(); 6 6 print( r1 ); … … 10 10 print( r3 ); 11 11 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; } 15 func fun( _ x : Double ) { 3; } 16 func fun( _ x : Int ) -> Int { 3; } 17 func fun( _ x : Int, _ y : Int ) -> Int { x + y; } 18 func fun( _ x : String ) -> String { "abc"; } 16 19 print( fun( 3, 4 ) ); 17 20 // print( fun( 3.5 ) ); // no Double -> Int … … 19 22 20 23 // overloading on parameter name 21 func foo( x : Int ) -> Int { 3; }22 func foo( y : Int ) -> Int { 3; }24 func foo( x : Int ) -> Int { 3; } 25 func foo( y : Int ) -> Int { 3; } 23 26 print( foo( x : 3 ) ); 24 27 print( foo( y : 3 ) ); 25 28 26 // overloading on generics 29 // overloading on member name 30 class 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 37 let c = C() 38 c.foo(); 39 r1 = c.foo( 3 ); 40 r3 = c.foo( 3, 3.5 ); 41 r2 = c.foo( "abc" ); 42 43 // generic overloading without parameter names 27 44 func bar<T>( _ a : T ) { print( "bar1", a ); } 28 45 func bar<T>( _ a : T, _ b : T ) { print( "bar2", a, b ); } … … 33 50 bar( 2, 2.5 ); 34 51 52 // generic overloading with parameter names 53 func baz( a : Int ) { print( "baz1", a ); } 54 func baz<T>( a : T ) { print( "baz1", a ); } 55 func baz<T>( a : T, b : T ) { print( "baz2", a, b ); } 56 func baz<T,U>( a : T, b : U ) { print( "baz3", a, b ); } 57 baz( a : 3 ); 58 baz( a : 3.5 ); 59 baz( a : 2, b : 2 ); 60 baz( a : 2, b : 2.5 ); 61 func swap<T>( _ a : inout T, _ b : inout T ) { let temp : T = a; a = b; b = temp; } 62 var ix = 4, iy = 3; 63 swap( &ix, &iy ); 64 print( ix, iy ); 65 var fx = 4.5, fy = 3.5; 66 swap( &fx, &fy ); 67 print( fx, fy ); 35 68 // Local Variables: // 36 69 // compile-command: "swift test.swift" //
Note:
See TracChangeset
for help on using the changeset viewer.