-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Closed as not planned
Description
Compiler version
If you're not sure what version you're using, run print scalaVersion from sbt
(if you're running scalac manually, use scalac -version instead).
3.3.4 & 3.6.2
Minimized code
Works code 1:
import scala.util.Try
import scala.concurrent.Future
import scala.concurrent.Awaitable
object CompletedFuture {
def unapply[T](f: Future[T]): Option[Try[T]] = f.value
}
def result[T](future: Awaitable[T]): T = future match {
case CompletedFuture(result:Try[T] @unchecked) => result.get
case _ => ???
}
println(result(Future.successful(1)))
Works code 2:
import scala.util.Try
import scala.concurrent.Future
import scala.concurrent.Awaitable
object CompletedFuture {
def unapply[T](f: Future[T]): Option[Try[T]] = f.value
}
def result[T](future: Awaitable[T]): T = future match {
case CompletedFuture(result:Try[T] ) => result.get
case _ => ???
}
println(result(Future.successful(1)))
Works code 3:
import scala.util.Try
import scala.concurrent.Future
import scala.concurrent.Awaitable
object CompletedFuture {
def unapply[T](f: Future[T]): Option[Try[T]] = f.value
}
def result[T](future: Future[T]): T = future match {
case CompletedFuture(result) => result.get
case _ => ???
}
println(result(Future.successful(1)))
Bad code :
import scala.util.Try
import scala.concurrent.Future
import scala.concurrent.Awaitable
object CompletedFuture {
def unapply[T](f: Future[T]): Option[Try[T]] = f.value
}
def result[T](future: Awaitable[T]): T = future match {
case CompletedFuture(result) => result.get
case _ => ???
}
println(result(Future.successful(1)))
https://scastie.scala-lang.org/He-Pin/2UboOKGuRp2CXe5XS3ZCXQ/1
Output
Found: Any
Required: TExpectation
Works as 2.13.x