lundi 12 octobre 2020

Type erasure problem in method overloading

I have two overloaded method having following signatures -

def fun(x: Seq[String]): Future[Seq[Int]] = ???
def fun(x: Seq[(String, String)]): Future[Seq[Int]] = ???

Due to type erasure, these methods can't be overloaded and hence showing compilation error. I tried using typetags as a workaround -

def fun[t: TypeTag](values: Seq[T]): Future[Seq[Int]] = {
    typeOf[T] match {
        case t if t =:= typeOf[String] => ???
        case t if t =:= typeOf[(String, String)] => ???
        case _ => ???    // Should not come here
     }
}

There are two problems with this approach that I am facing -

  1. How can I use the values in the Seq ?
  2. How can I avoid Should not come here case ?

Thanks in advance.





Aucun commentaire:

Enregistrer un commentaire