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/neg/sammy_overload.scala | |
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/neg/sammy_overload.scala')
-rw-r--r-- | test/files/neg/sammy_overload.scala | 12 |
1 files changed, 7 insertions, 5 deletions
diff --git a/test/files/neg/sammy_overload.scala b/test/files/neg/sammy_overload.scala index 91c52cf96c..548e9d2d2e 100644 --- a/test/files/neg/sammy_overload.scala +++ b/test/files/neg/sammy_overload.scala @@ -2,12 +2,14 @@ trait ToString { def convert(x: Int): String } class ExplicitSamType { object O { - def m(x: Int => String): Int = 0 - def m(x: ToString): Int = 1 + def m(x: Int => String): Int = 0 // (1) + def m(x: ToString): Int = 1 // (2) } - O.m((x: Int) => x.toString) // ok, function type takes precedence + O.m((x: Int) => x.toString) // ok, function type takes precedence, because (1) is more specific than (2), + // because (1) is as specific as (2): (2) can be applied to a value of type Int => String (well, assuming it's a function literal) + // but (2) is not as specific as (1): (1) cannot be applied to a value of type ToString - O.m(_.toString) // error expected: eta-conversion breaks down due to overloading - O.m(x => x) // error expected: needs param type + O.m(_.toString) // ok: overloading resolution pushes through `Int` as the argument type, so this type checks + O.m(x => x) // error expected: m cannot be applied to Int => Int } |