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

Context bound type parameter in def with explicit return type does not compile #1174

Closed
agboom opened this issue Mar 14, 2016 · 0 comments
Closed

Comments

@agboom
Copy link

agboom commented Mar 14, 2016

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.

odersky added a commit to dotty-staging/dotty that referenced this issue Mar 21, 2016
Rewriting did not go far enough, as evidenced by pos/i1174.scala

Fixes scala#1174
smarter referenced this issue Mar 22, 2016
Fix problem involving classtag based pattern matches.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant