Closed
Description
The following code does not compile:
import scala.reflect.ClassTag
import scala.util._
object Main {
class A
def constructAs[T <: A: ClassTag]: Try[T] = Try {
new A()
}.flatMap {
case inst: T => Success(inst)
case _ =>
val tag = implicitly[ClassTag[T]]
Failure(new ClassCastException(s"Failed to construct instance of class ${tag.runtimeClass.getName}"))
}
}
Dotc gives the following error:
Main.scala:10: error: type mismatch:
found : Main.A(inst)
required: Nothing'
case inst: T => Success(inst)
^
one error found
If I leave out the context bound, the code compiles fine. In Scala 2.11.8 it compiles too.
Weirdly enough, if I leave out the explicit return type, it also compiles fine.
Metadata
Metadata
Assignees
Labels
No labels