diff options
author | Lukas Rytz <lukas.rytz@epfl.ch> | 2009-05-30 07:36:31 +0000 |
---|---|---|
committer | Lukas Rytz <lukas.rytz@epfl.ch> | 2009-05-30 07:36:31 +0000 |
commit | 390ccacfe0caa4c07af6193dec3e172c0fcd7896 (patch) | |
tree | 001ff4a00bd9d8cab651d9bf245bfc795748d829 /test/files/run/names-defaults.scala | |
parent | 661f1ba10e5062fd987c4cafe43ad1f0dc3f5491 (diff) | |
download | scala-390ccacfe0caa4c07af6193dec3e172c0fcd7896.tar.gz scala-390ccacfe0caa4c07af6193dec3e172c0fcd7896.tar.bz2 scala-390ccacfe0caa4c07af6193dec3e172c0fcd7896.zip |
Named and default arguments
- MethodTypes now have (params: List[Symbol])
- "copy"-methods for case classes
- the "copy" object in the compiler is now called "treeCopy"
Diffstat (limited to 'test/files/run/names-defaults.scala')
-rw-r--r-- | test/files/run/names-defaults.scala | 222 |
1 files changed, 222 insertions, 0 deletions
diff --git a/test/files/run/names-defaults.scala b/test/files/run/names-defaults.scala new file mode 100644 index 0000000000..01eebd42f8 --- /dev/null +++ b/test/files/run/names-defaults.scala @@ -0,0 +1,222 @@ +object Test extends Application { + def get[T](x: T) = { println("get: "+ x); x } + + // TESTS + + // re-order using names, call-site evaluation order + test1(1, "@") + test1(b = get("$"), a = get(2)) + test1(a = get(3), b = get("**")) // should not transform into a block. how to test? + test3(b = get(110), a = get(11))(c = get("\\"), d = get(2.399)) + test3(get(14), get(3920))(d = get("}"), c = get("[")) + + + // mixing named and positional + test1(get(4), b = get("@")) + test2(get(8), v = get(9))(get("%"), l = get(5)) + test3(12, 13)("'", d = 16) + + + // anonymous functions + val f1: (Int, String) => Unit = test1(_, _); f1(6, "~") + val f2: Int => Unit = test1(a = _, b = get("+")); f2(get(7)) + val f3 = test1(b = _: String, a = get(8)); f3(get("+")) + val f4: (Int, String) => Unit = test1(_, b = _); f4(9, "?") + + val f5: Int => (String, Int) => Unit = test2(v = get(38), u = _)_ + f5(get(39))(get("|"), 10) + + val f6: (Double, String) => Unit = test3(get(13), _)(d = _, c = get("x")) + f6(get(2.233), get("<")) + + + test4(14) + + + // defaults: subclass overrides, adds and inherits default + val b = new Base + b.test1(b = "nix")(982)(f = 0) + val s = new Sub1 + s.test1(a = new { override def toString = "bla" })(m = 0)() + + // defaults are chosen dynamically + val b2: Base = new Sub1 + b2.test1(b = "")(c = 93.3)(f = -1) + + + + // overloading resolution + object t1 { + def f(a: Int, b: String) = "first" + def f(b: String, a: Int) = "second" + } + println(t1.f(1, "2")) // first + + object t2 { + def f(a: Int, b: Double, c: Object) = "first" + def f(a: Int, b: Double, c: String) = "second" + } + println(t2.f(1, c = new Base(), b = 2.2)) // first + println(t2.f(28, b = 3.89, c = "ldksfj")) // second + + object t3 { + def f(a1: Int) = "first" + def f(a2: Int)(b: Int) = "second" + } + println(t3.f(a1 = 10)) // first + println(t3.f(a2 = 20)(1)) // second + + object t4 { + def f(a: Int, b: String = "foo") = "first" + def f(a: Int) = "second" + } + println(t4.f(109)) // second + println(t4.f(a = 20)) // second + + object t5 { + def f(a: Object) = "first" + val f: String => String = a => "second" + } + println(t5.f(new Sub1())) // firsst + println(t5.f("dfklj")) // second + + object t6 { + def f(a: String = "sdf", b: Int) = "f" + def f(a: Int, b: Int) = "s" + } + println(t6.f(b = 289)) // f + + object t7 { + def f(a: Int, b: String*) = "first" + def f(a: Int) = "second" + def g(a: Sub1, b: Int*) = "third" + def g(a: Base) = "fourth" + def h(a: Base, b: Int*) = "fifth" + def h(a: Sub1) = "sixth" + } + println(t7.f(1)) // second + println(t7.f(a = 19)) // second + println(t7.f(b = "sl19", a = 28)) // first + println(t7.g(new Sub1(), 1, 2)) // third + println(t7.g(new Base())) // fourth + println(t7.h(new Base())) // fifth + println(t7.h(new Sub1())) // sixth + + object t9 { + def f(a: String, b: Int = 11) = "first" + def f(a: Double) = "second" + } + println(t9.f("bla")) // first + + + // vararg + def test5(a: Int, b: Int)(c: Int, d: String*) = a +", "+ d.toList + println(test5(b = 1, a = 2)(3, "4", "4", "4")) + println(test5(b = 1, a = 2)(c = 29)) + + + // tuple conversion + def foo(a: Int, b: Int)(c: (Int, String)) = a + c._1 + println(foo(b = 1, a = 2)(3, "4")) + + + // by-name parameters + def bn1(a: Int, b: => Int) = a + println(bn1(b = get(10), a = get(11))) // should not see get(10) + + def bn2(a: Int, b: => Int)(c: Int = b) = a + b + println(bn2(b = get(2), a = get(1))()) // should get: 1, 2, 2 + + + // constructors + val a1 = new A(b = "dlkfj")(d = 102) + println(a1.print) + val a2 = new A[String, Nothing](2, "dkflj")(d = 2, c = "lskf") + println(a2.print) + val b1 = new B("dklfj")(e = "nixda") + println(b1.printB) + val c1 = new C(a = "dlkf", c = new { override def toString() = "struct" })(e = "???") + println(c1.print) + val c2 = C("dflkj", c = Some(209): Option[Int])(None, "!!") + println(c2.print) + val a_f: String => A[String, Nothing] = new A[String, Nothing](b = _)(d = 100) + println(a_f("20").print) + val c_f: Int => C[Int] = C("dlfkj", c = 10, b = _)(35, e = "dlkf") + println(c_f(11).print) + + + // "super" qualifier + val b10 = new B1 + println(b10.bar()) + + + // defaults in traits / abstract classes + val mn = new MN + println(mn.foo()()) + println(mn.bar(10)) + // anonymous class + println((new M { def foo[T >: String](x: Int, y: T)(z: String = "2") = z ; def bar(x: Int, y: Double) = x }).foo()()) + + // copy method for case classes + val fact = Factory(y = "blabla")() + println(fact) + println(fact.copy(x = -1)("dldl")) + + println(Fact2()("jyp")) + println(Fact2(x = 1)()) + println(Fact2(10)().copy(y = "blabla")()) + + + // DEFINITIONS + def test1(a: Int, b: String) = println(a +": "+ b) + def test2(u: Int, v: Int)(k: String, l: Int) = println(l +": "+ k +", "+ (u + v)) + + def test3[T1, T2](a: Int, b: T1)(c: String, d: T2) = println(a +": "+ c +", "+ b +", "+ d) + + def test4(a: Int) = { + def inner(b: Int = a, c: String) = println(b +": "+ c) + inner(c = "/") + } +} + + +class Base { + def test1[T1, T2](a: Int = 100, b: T1)(c: T2, d: String = a +": "+ b)(e: T2 = c, f: Int) = + println(a +": "+ d +", "+ b +", "+ c +", "+ e +", "+ f) +} + +class Sub1 extends Base { + override def test1[U1, U2](b: Int, a: U1)(m: U2, r: String = "overridden")(o: U2, f: Int = 555) = + println(b +": "+ r +", "+ a +", "+ m +", "+ o +", "+ f) +} + + +class A[T <: String, U](a: Int = 0, b: T)(c: String = b, d: Int) { def print = c + a + b + d } +class B[T](a: T, b: Int = 1)(c: T = a, e: String = "dklsf") extends A(5, e)("dlkd", 10) { def printB = super.print + e + a + b + c } + +case class C[U](a: String, b: Int = 234, c: U)(d: U = c, e: String = "dlkfj") { def print = toString + d + e } + + +class A1 { + def foo(a: Int = 10, b: String) = b + a +} +class B1 extends A1 { + def bar(a: String = "dflk") = super.foo(b = a) +} + +trait N { + def foo[T >: String](x: Int = -1, y: T = "jupee")(z: String): String +} + +abstract class M extends N { + def foo[T >: String](x: Int, y: T)(z: String = "1"): String + def bar(n: Int, m: Double = 1.239): Double +} + +class MN extends M { + def foo[T >: String](x: Int, y: T)(z: String) = z + x + y + def bar(n: Int, m: Double) = n*m +} + +case class Factory(x: Int = 1, y: String)(z: String = y) +case class Fact2[T, +U](x: T = "ju", y: U = 1)(z: T = 2) |