没有推断出类型参数

时间:2016-08-10 10:08:24

标签: scala

在此代码段中调用Nothing的类型参数是get怎么可能?在没有显式类型参数的情况下调用case class User(email: String) object Hello { def main(args: Array[String]): Unit = { val store = new ObjectStore store.get } } class ObjectStore { def get[A: Manifest]: Option[A] = { println(manifest[A].toString()) None } } 时,如何强制编译器产生错误?

jsdom.env

1 个答案:

答案 0 :(得分:2)

基于this blog post,以下内容应该有效:

@implicitNotFound("Nothing was inferred")
sealed trait NotNothing[-T]
object NotNothing {
  implicit object notNothing extends NotNothing[Any]
  implicit object `\n The error is because the type parameter was resolved to Nothing` extends NotNothing[Nothing]
}

class ObjectStore {
  def get[T](implicit evManifest: Manifest[T], evNotNothing: NotNothing[T]): Option[T] = {
    println(manifest[T].toString())
    None
  }
}

object X {
  val oo = new ObjectStore().get[Any]
  //fails to compile
  //val o = new ObjectStore().get
}