From 0e170e4b695adafb3f3d5823026ccf8d10047be3 Mon Sep 17 00:00:00 2001 From: Lukas Rytz Date: Sat, 20 Jun 2009 08:19:40 +0000 Subject: improvements to names / defaults (implicits, ty... improvements to names / defaults (implicits, type of defaults, #2064, ...) --- test/files/neg/bug550.check | 2 +- test/files/neg/names-defaults-neg.check | 41 +++++++++++++++++++------------ test/files/neg/names-defaults-neg.scala | 21 +++++++++++++--- test/files/neg/t0226.check | 2 +- test/files/run/names-defaults.check | 9 ++++++- test/files/run/names-defaults.scala | 43 +++++++++++++++++++++++++++++++++ 6 files changed, 97 insertions(+), 21 deletions(-) (limited to 'test/files') diff --git a/test/files/neg/bug550.check b/test/files/neg/bug550.check index 1cdb7931ca..bf92f6bb05 100644 --- a/test/files/neg/bug550.check +++ b/test/files/neg/bug550.check @@ -1,7 +1,7 @@ bug550.scala:6: error: type List takes type parameters def sum[a](xs: List)(implicit m: Monoid[a]): a = ^ -bug550.scala:8: error: no implicit argument matching parameter type Monoid[a] was found. +bug550.scala:8: error: could not find implicit value for parameter m:Monoid[a]. sum(List(1,2,3)) ^ two errors found diff --git a/test/files/neg/names-defaults-neg.check b/test/files/neg/names-defaults-neg.check index b208157c6a..f1db7676dd 100644 --- a/test/files/neg/names-defaults-neg.check +++ b/test/files/neg/names-defaults-neg.check @@ -1,4 +1,5 @@ -names-defaults-neg.scala:63: error: not enough arguments for method apply: (a: Int,b: String)(c: Int*)Fact in object Fact, unspecified parameter: value b +names-defaults-neg.scala:63: error: not enough arguments for method apply: (a: Int,b: String)(c: Int*)Fact in object Fact. +Unspecified value parameter b. val fac = Fact(1)(2, 3) ^ names-defaults-neg.scala:5: error: type mismatch; @@ -71,23 +72,33 @@ and method f in object t8 of type (a: Int,b: java.lang.Object)java.lang.String match argument types (a: Int,b: java.lang.String) and expected result type Any println(t8.f(a = 0, b = "1")) // ambigous reference ^ -names-defaults-neg.scala:75: error: multiple overloaded alternatives of method foo define default arguments +names-defaults-neg.scala:67: error: wrong number of arguments for : (x: Int,y: String)A1 + A1() match { case A1(_) => () } + ^ +names-defaults-neg.scala:74: error: inferred kinds of the type arguments (List[Int]) do not conform to the expected kinds of the type parameters (type T). +List[Int]'s type parameters do not match type T's expected parameters: class List has one type parameter, but type T has one + test4() + ^ +error: type mismatch; + found : List[Int] + required: List[List[List[List[X forSome { type X }]]]] +names-defaults-neg.scala:77: error: type mismatch; + found : List[Int] + required: List[List[?]] + def test6[T](x: List[List[T]] = List(1,2)) = x + ^ +names-defaults-neg.scala:80: error: type mismatch; + found : Int + required: String + new A2[String]() + ^ +names-defaults-neg.scala:90: error: multiple overloaded alternatives of method foo define default arguments def foo(a: Int = 0) = a ^ -names-defaults-neg.scala:85: error: multiple overloaded alternatives of method foo define default arguments +names-defaults-neg.scala:100: error: multiple overloaded alternatives of method foo define default arguments override def foo(a: Int = 1092) = a ^ -names-defaults-neg.scala:88: error: type mismatch; - found : Int(129083) - required: java.lang.String - def bar(i: Int = 129083) = i - ^ -names-defaults-neg.scala:88: error: multiple overloaded alternatives of method bar define default arguments +names-defaults-neg.scala:103: error: multiple overloaded alternatives of method bar define default arguments def bar(i: Int = 129083) = i ^ -names-defaults-neg.scala:95: error: type mismatch; - found : java.lang.Object - required: java.lang.String -class B5 extends A5 { override def foo(a: Object = new Object) = 1 } - ^ -22 errors found \ No newline at end of file +25 errors found diff --git a/test/files/neg/names-defaults-neg.scala b/test/files/neg/names-defaults-neg.scala index 68cdcf782f..2e9f755174 100644 --- a/test/files/neg/names-defaults-neg.scala +++ b/test/files/neg/names-defaults-neg.scala @@ -63,6 +63,21 @@ object Test extends Application { val fac = Fact(1)(2, 3) val facc = fac.copy(b = "dlkfj")() + // no defaults in patterns + A1() match { case A1(_) => () } + + + // return types of default getters + + // definition compiles, but default cannot be used, it doesn't conform + def test4[T[P]](x: T[T[List[T[X forSome { type X }]]]] = List(1,2)) = x + test4() + + // doesn't compile + def test6[T](x: List[List[T]] = List(1,2)) = x + + // correct error message + new A2[String]() // DEFINITIONS def test1(a: Int, b: String) = a +": "+ b @@ -90,6 +105,6 @@ class C extends B { case class Fact(a: Int, b: String)(c: Int*) -// overriding default must have same type -class A5 { def foo(a: Object = "dlkf") = 0 } -class B5 extends A5 { override def foo(a: Object = new Object) = 1 } +case class A1(x: Int = 1, y: String = "2") + +class A2[T](a: T = 1) diff --git a/test/files/neg/t0226.check b/test/files/neg/t0226.check index 0935a3b14e..8464d72914 100644 --- a/test/files/neg/t0226.check +++ b/test/files/neg/t0226.check @@ -4,7 +4,7 @@ t0226.scala:5: error: not found: type A1 t0226.scala:5: error: not found: type A1 (implicit _1: Foo[List[A1]], _2: Foo[A2]): Foo[Tuple2[List[A1], A2]] = ^ -t0226.scala:8: error: no implicit argument matching parameter type Test.this.Foo[((List[Char], Int), (object Nil, Int))] was found. +t0226.scala:8: error: could not find implicit value for parameter rep:Test.this.Foo[((List[Char], Int), (object Nil, Int))]. foo(((List('b'), 3), (Nil, 4))) ^ three errors found diff --git a/test/files/run/names-defaults.check b/test/files/run/names-defaults.check index 07a7d7d1be..e8c088fe7a 100644 --- a/test/files/run/names-defaults.check +++ b/test/files/run/names-defaults.check @@ -97,4 +97,11 @@ slkdfj1 lskfdjlk 11 2 -20 \ No newline at end of file +20 +10 +jaa +kldfj110101 +klfj1 +blublu1 +my text +List(1, 2) diff --git a/test/files/run/names-defaults.scala b/test/files/run/names-defaults.scala index 6ec391af2f..3efefc9e71 100644 --- a/test/files/run/names-defaults.scala +++ b/test/files/run/names-defaults.scala @@ -198,6 +198,40 @@ object Test extends Application { new B4() new B5() + // no re-naming of parameters which are free in a closure of the body (lambdalift) + println(test6(10)()) + test7("jaa") + + // implicits + defaults + { + implicit val implInt = 10101 + println(test8()) + } + + println(test9) + + { + implicit val implString = "blublu" + println(test9) + } + + + // result type of default getters: parameter type, except if this one mentions any type + // parameter, in which case the result type is inferred. examples: + + // result type of default getter is "String => String". if it were infered, the compiler + // would put "Nothing => Nothing", which is useless + def transform(s: String, f: String => String = identity _) = f(s) + println(transform("my text")) + + // result type of the default getter is inferred (parameter type mentions type parameter T) + def test10[T](x: List[T] = List(1,2)) = x + println(test10()) + + // some complicated type which mentions T + def test11[T[P]](x: T[T[List[T[X forSome { type X }]]]] = List(1,2)) = x + // (cannot call f using the default, List(1,2) doesn't match the param type) + // DEFINITIONS def test1(a: Int, b: String) = println(a +": "+ b) @@ -210,6 +244,11 @@ object Test extends Application { inner(c = "/") } def test5(argName: Unit) = println("test5") + def test6(x: Int) = { () => x } + def test7(s: String) = List(1).foreach(_ => println(s)) + + def test8(x: Int = 1)(implicit y: Int, z: String = "kldfj") = z + x + y + def test9(implicit x: Int = 1, z: String = "klfj") = z + x } @@ -288,3 +327,7 @@ class B4 extends A5(10)() { class B5 extends A5(y = 20, x = 2)() { println(y) } + +// overriding default can be less specific (but has to conform to argument type!) +class A6 { def foo(a: Object = "dlkf") = 0 } +class B6 extends A6 { override def foo(a: Object = new Object) = 1 } -- cgit v1.2.3