I am trying to implement a simple validator method that would take a type as its first parameter and a String as the second and would always return an instance of the passed-in type. Without no further ado - this is the method:
def validateType[T](taip: T, input: String):T = taip match {
case Boolean => {
val simplifiedInput = input.replaceAll("[ \\n]", "").toLowerCase()
if(simplifiedInput.matches("[yn]")){
simplifiedInput match {
case "y" => true
case "n" => false
}
} else{
validateType(Boolean, StdIn.readLine("$simplifiedInput is not a valid "
+"answer, please try again"))
}
}
}
However I get a compile error (red underscore in my IDE) saying: "type mismatch; Found Boolean(true) required : T
I realize that T
here is a type
, but how can I specify that I want to return an instance of that type? I tried to play with the ClassTag also but with no success. Thank you.
Aucun commentaire:
Enregistrer un commentaire