简体   繁体   English

没有推断出类型参数

[英]Nothing inferred for type parameter

How is it possible that type parameter in call to get[A] is Nothing in this snippet? 在此代码段中,对get[A]调用中的type参数怎么可能Nothing What can I do to force a compiler to produce an error when get is called without explicit type parameter? 在没有显式类型参数的情况下调用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
  }
}

Based on this blog post , the following should work: 根据此博客文章 ,以下应该起作用:

@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
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM