diff options
author | Adriaan Moors <adriaan@lightbend.com> | 2016-07-27 11:18:08 -0700 |
---|---|---|
committer | Adriaan Moors <adriaan@lightbend.com> | 2016-08-12 16:39:31 -0700 |
commit | 228d1b1ceb7643df1313672cd620cddb1a429029 (patch) | |
tree | 4fb946f9fe8a03cf759f194b94e17c8544bbd4fb /test/files/pos | |
parent | 618d42c747955a43557655bdc0c4281fec5a7923 (diff) | |
download | scala-228d1b1ceb7643df1313672cd620cddb1a429029.tar.gz scala-228d1b1ceb7643df1313672cd620cddb1a429029.tar.bz2 scala-228d1b1ceb7643df1313672cd620cddb1a429029.zip |
Propagate overloaded function type to expected arg type
Infer missing parameter types for function literals passed
to higher-order overloaded methods by deriving the
expected argument type from the function types in the
overloaded method type's argument types.
This eases the pain caused by methods becoming overloaded
because SAM types and function types are compatible,
which used to disable parameter type inference because
for overload resolution arguments are typed without
expected type, while typedFunction needs the expected
type to infer missing parameter types for function literals.
It also aligns us with dotty. The special case for
function literals seems reasonable, as it has precedent,
and it just enables the special case in typing function
literals (derive the param types from the expected type).
Since this does change type inference, you can opt out
using the Scala 2.11 source level.
Fix scala/scala-dev#157
Diffstat (limited to 'test/files/pos')
-rw-r--r-- | test/files/pos/overloaded_ho_fun.scala | 51 |
1 files changed, 51 insertions, 0 deletions
diff --git a/test/files/pos/overloaded_ho_fun.scala b/test/files/pos/overloaded_ho_fun.scala new file mode 100644 index 0000000000..2699ad35f8 --- /dev/null +++ b/test/files/pos/overloaded_ho_fun.scala @@ -0,0 +1,51 @@ +import scala.math.Ordering +import scala.reflect.ClassTag + +trait Sam { def apply(x: Int): String } +trait SamP[U] { def apply(x: Int): U } + +class OverloadedFun[T](x: T) { + def foo(f: T => String): String = f(x) + def foo(f: Any => T): T = f("a") + + def poly[U](f: Int => String): String = f(1) + def poly[U](f: Int => U): U = f(1) + + def polySam[U](f: Sam): String = f(1) + def polySam[U](f: SamP[U]): U = f(1) + + // check that we properly instantiate java.util.function.Function's type param to String + def polyJavaSam(f: String => String) = 1 + def polyJavaSam(f: java.util.function.Function[String, String]) = 2 +} + +class StringLike(xs: String) { + def map[A](f: Char => A): Array[A] = ??? + def map(f: Char => Char): String = ??? +} + +object Test { + val of = new OverloadedFun[Int](1) + + of.foo(_.toString) + + of.poly(x => x / 2 ) + of.polySam(x => x / 2 ) + of.polyJavaSam(x => x) + + val sl = new StringLike("a") + sl.map(_ == 'a') // : Array[Boolean] + sl.map(x => 'a') // : String +} + +object sorting { + def stableSort[K: ClassTag](a: Seq[K], f: (K, K) => Boolean): Array[K] = ??? + def stableSort[L: ClassTag](a: Array[L], f: (L, L) => Boolean): Unit = ??? + + stableSort(??? : Seq[Boolean], (x: Boolean, y: Boolean) => x && !y) +} + +// trait Bijection[A, B] extends (A => B) { +// def andThen[C](g: Bijection[B, C]): Bijection[A, C] = ??? +// def compose[T](g: Bijection[T, A]) = g andThen this +// } |