Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Eta-expand when a partial function is expected #11717

dwijnand opened this issue Aug 29, 2019 · 1 comment


Copy link

commented Aug 29, 2019

Since scala/scala#8172 (targeting 2.13.1) it's possible to define an anonymous function where a PartialFunction is expected.

Methods should also eta-expand for that.

Welcome to Scala 2.13.1-20190829-082322-dfde51f (OpenJDK 64-Bit GraalVM CE 19.1.1, Java 1.8.0_222).
Type in expressions for evaluation. Or try :help.

scala> import scala.util.Try
import scala.util.Try

scala> Try[Int](???).recover(_ => 1)
res0: scala.util.Try[Int] = Success(1)

scala> val fallbackV = (_: Throwable) => 1
fallbackV: Throwable => Int = $$Lambda$2264/1283050069@3a63d248

scala> Try[Int](???).recover(fallbackV)
       error: type mismatch;
        found   : Throwable => Int
        required: PartialFunction[Throwable,?]

scala> Try[Int](???).recover(fallbackV(_))
res2: scala.util.Try[Int] = Success(1)

scala> def fallbackM(_x: Throwable) = 1
fallbackM: (_x: Throwable)Int

scala> Try[Int](???).recover(fallbackM)
       error: missing argument list for method fallbackM
       Unapplied methods are only converted to functions when a function type is expected.
       You can make this conversion explicit by writing `fallbackM _` or `fallbackM(_)` instead of `fallbackM`.

scala> Try[Int](???).recover(fallbackM(_))
res4: scala.util.Try[Int] = Success(1)

I'm referring to the case Try[Int](???).recover(fallbackM).


This comment has been minimized.

Copy link

commented Aug 30, 2019

Almost as good as making Function1 extend PartialFunction

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
None yet
2 participants
You can’t perform that action at this time.