Reputation: 3058
For some reason I have
val stuff: Map[String, Any] = Map[String, Any](
("a", 1),
("b", "one"),
("c", false)
)
def getThing[T](key: String): T = {
stuff.get(key).get.asInstanceOf[T]
}
val a: Int = getThing("a") // I want this to break on compile
val anotherA: Int = getThing[Int]("a") // I want this to work as normal
I want the get's without specifying the type to break on compile, and the ones that do specify to work.
Upvotes: 2
Views: 139
Reputation: 15086
You can't force a type argument to be provided explicitly. Maybe you can turn it into a normal argument, if you really want this behaviour...
case class Type[T]
def getThing[T](t: Type[T])(key: String): T =
stuff.get(key).get.asInstanceOf[T]
val a = getThing(Type[Int])("a")
Upvotes: 1