Scala 没有为类型参数推断任何内容
调用Scala 没有为类型参数推断任何内容,scala,Scala,调用get[A]的类型参数怎么可能在这个代码段中是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]
get[A]
的类型参数怎么可能在这个代码段中是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
}
}
基于此,应采取以下措施:
@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
}