2016-08-10 4 views
1

Как возможно, что параметр типа в вызове 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 
    } 
} 

ответ

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 
}