5

有没有办法用泛型泛化这段代码?

object ListInt {
  def unapply(o: Any): Option[List[Int]] = o match {
    case lst: List[_] if(lst.forall(_.isInstanceOf[Int])) => 
      Some(lst.asInstanceOf[List[Int]])
    case _ => None
  }
}
object ListDouble {
  def unapply(o: Any): Option[List[Double]] = o match {
    case lst: List[_] if(lst.forall(_.isInstanceOf[Double])) =>
      Some(lst.asInstanceOf[List[Double]])
    case _ => None
  }
}
object ListString {
  def unapply(o: Any): Option[List[String]] = o match {
    case lst: List[_] if(lst.forall(_.isInstanceOf[String])) =>
      Some(lst.asInstanceOf[List[String]])
    case _ => None
  }
}

val o: Any = List("a", "b", "c")
o match {
  case ListInt(lst) => println(lst.sum)
  case ListDouble(lst) => println(lst.product)
  case ListString(lst) => println(lst.mkString("(", ", ", ")"))
  case _ => println("no match")
}
4

2 回答 2

6
abstract class ListExtractor[A](implicit ct: reflect.ClassTag[A]) {
  def unapply(o: Any): Option[List[A]] = o match {
    case lst: List[_] if (lst.forall(ct.unapply(_).isDefined)) =>
      Some(lst.asInstanceOf[List[A]])
    case _ => None
  }
}

object ListInt    extends ListExtractor[Int   ]
object ListString extends ListExtractor[String]

val o: Any = List("a", "b", "c")
o match {
  case ListInt   (lst) => println(lst.sum)
  case ListString(lst) => println(lst.mkString("(", ", ", ")"))
  case _               => println("no match")
}
于 2013-06-02T19:52:38.537 回答
3

看来 TypeTag 是要走的路:

import scala.reflect.runtime.universe._

def foo[A: TypeTag](lst: A) = typeOf[A] match {
  case t if t =:= typeOf[List[Int]] => lst.asInstanceOf[List[Int]].sum
  case t if t =:= typeOf[List[Double]] => lst.asInstanceOf[List[Double]].product
  case t if t =:= typeOf[List[String]] => lst.asInstanceOf[List[String]].mkString("(", ", ", ")")
}

println(foo(List("a", "b", "c")))

查看这篇出色的帖子以获取详细说明:

Scala:什么是 TypeTag,我如何使用它?

于 2013-06-02T19:54:55.513 回答