source: doc/theses/fangren_yu_MMath/test.scala

Last change on this file was d92bc97, checked in by Peter A. Buhr <pabuhr@…>, 4 months ago

add Scala overloading test-program

  • Property mode set to 100644
File size: 1.2 KB
Line 
1object Main {
2 def fun( x: Int ) : Int = { print( "Int " ); return 3 }
3 def fun( x: Double ) : Int = { print( "Double " ); return 3 }
4 def fun( x: Int, y: Int ) : Int = { print( "Int, Int " ); return 3 }
5 def fun() : Int = { print( "() " ); return 3 }
6 def fun() : Double = { return 3.5 } // disallowed, ambiguous
7 def fun[T]( x: T ) : T = { print( "T " ); return x }
8 def fun[T,T]( x: T, y: T ) : T = { print( "T,T " ); return y }
9 def fun[T,U]( x: T, y: U ) : U = { print( "T,U " ); return y } // disallowed, ambiguous
10 val i: int = fun( 3, 3 );
11 val d2: double = fun( 3, 3.5 );
12 val d: Double = fun( 3.5 )
13 val s: String = fun( "def" )
14 def main( args: Array[String] ) = {
15 println( fun( 3 ) )
16 println( fun( 3.5 ) )
17 println( fun( 3, 3 ) )
18 println( fun() )
19 println( fun( "abc" ) )
20 }
21
22 // // https://scastie.scala-lang.org/CuNcusyPTUKgZiVNWdyqZA
23 // needs scala 3.0
24
25 // trait HasFoo[T] { def foo: T }
26
27 // given HasFoo[String]:
28 // def foo = "string"
29 // given HasFoo[Int]:
30 // def foo = 42
31
32 // def generic[T](using hasFoo: HasFoo[T]): T = hasFoo.foo
33
34 // val i: Int = generic
35 // val s: String = generic
36
37 // println(i) (): scala.Unit
38 // println(s) (): scala.Unit
39}
40
41// scalac test.scala
Note: See TracBrowser for help on using the repository browser.