I'm trying to write a function like this:
def checkType[T: ClassTag](value: Any, t: ClassTag[T]): ValidationNel[String, T] = value match {
case v: T => Success(v.asInstanceOf[T])
case _ => Failure("Incorrect type").toValidationNel
}
I'm trying to call the function like so
val x: Any = 1
checkType(1, classOf[Int])
The function should take an x of type Any. As a second parameter it should take the type x should be checked against. If successful, it should return a properly typed x.
The above function compiles and works for classOf[String]. However it fails for Ints and Floats. Is it possible to write a generic function like this? Or do I have to maybe implement separate functions for each type that I want to check for, like a checkForString and checkForInt?
Any help would be greatly appreciated.
Aucun commentaire:
Enregistrer un commentaire