Skip to content

Type computed to Any, expect T #22304

@He-Pin

Description

@He-Pin

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

image

Output

Found:    Any
Required: T

Expectation

Works as 2.13.x

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions