@@ -1616,7 +1616,7 @@ trait Applications extends Compatibility {
16161616 * called twice from the public `resolveOverloaded` method, once with
16171617 * implicits and SAM conversions enabled, and once without.
16181618 */
1619- private def resolveOverloaded (alts : List [TermRef ], pt : Type , targs : List [Type ])(implicit ctx : Context ): List [TermRef ] = {
1619+ private def resolveOverloaded (alts : List [TermRef ], pt : Type , targs : List [Type ])(implicit ctx : Context ): List [TermRef ] =
16201620 record(" resolveOverloaded/2" )
16211621
16221622 def isDetermined (alts : List [TermRef ]) = alts.isEmpty || alts.tail.isEmpty
@@ -1785,28 +1785,42 @@ trait Applications extends Compatibility {
17851785 candidates.flatMap(cloneCandidate)
17861786 }
17871787
1788+ def resultIsMethod (tp : Type ): Boolean = tp.widen.stripPoly match
1789+ case tp : MethodType => tp.resultType.isInstanceOf [MethodType ]
1790+ case _ => false
1791+
17881792 val found = narrowMostSpecific(candidates)
17891793 if (found.length <= 1 ) found
1790- else pt match {
1791- case pt @ FunProto (_, resType : FunProto ) =>
1792- // try to narrow further with snd argument list
1793- val advanced = advanceCandidates(pt.typedArgs().tpes)
1794- resolveOverloaded(advanced.map(_._1), resType, Nil ) // resolve with candidates where first params are stripped
1795- .map(advanced.toMap) // map surviving result(s) back to original candidates
1796- case _ =>
1797- val noDefaults = alts.filter(! _.symbol.hasDefaultParams)
1798- val noDefaultsCount = noDefaults.length
1799- if (noDefaultsCount == 1 )
1800- noDefaults // return unique alternative without default parameters if it exists
1801- else if (noDefaultsCount > 1 && noDefaultsCount < alts.length)
1802- resolveOverloaded(noDefaults, pt, targs) // try again, dropping defult arguments
1803- else {
1804- val deepPt = pt.deepenProto
1805- if (deepPt ne pt) resolveOverloaded(alts, deepPt, targs)
1806- else candidates
1807- }
1808- }
1809- }
1794+ else
1795+ val deepPt = pt.deepenProto
1796+ deepPt match
1797+ case pt @ FunProto (_, resType : FunProto ) =>
1798+ // try to narrow further with snd argument list
1799+ val advanced = advanceCandidates(pt.typedArgs().tpes)
1800+ resolveOverloaded(advanced.map(_._1), resType, Nil ) // resolve with candidates where first params are stripped
1801+ .map(advanced.toMap) // map surviving result(s) back to original candidates
1802+ case _ =>
1803+ // prefer alternatives that need no eta expansion
1804+ val noCurried = alts.filter(! resultIsMethod(_))
1805+ val noCurriedCount = noCurried.length
1806+ if noCurriedCount == 1 then
1807+ noCurried
1808+ else if noCurriedCount > 1 && noCurriedCount < alts.length then
1809+ resolveOverloaded(noCurried, pt, targs)
1810+ else
1811+ // prefer alternatves that match without default parameters
1812+ val noDefaults = alts.filter(! _.symbol.hasDefaultParams)
1813+ val noDefaultsCount = noDefaults.length
1814+ if noDefaultsCount == 1 then
1815+ noDefaults
1816+ else if noDefaultsCount > 1 && noDefaultsCount < alts.length then
1817+ resolveOverloaded(noDefaults, pt, targs)
1818+ else if deepPt ne pt then
1819+ // try again with a deeper known expected type
1820+ resolveOverloaded(alts, deepPt, targs)
1821+ else
1822+ candidates
1823+ end resolveOverloaded
18101824
18111825 /** Try to typecheck any arguments in `pt` that are function values missing a
18121826 * parameter type. If the formal parameter types corresponding to a closure argument
0 commit comments