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 
} 
関連する問題