我知道什麼是List [_]基於清單我已經傳入一個方法,但我需要知道列表是什麼樣的項目。這些信息是否存儲在清單中的某個地方,並且可以將其清除?如果沒有,有關如何解決該問題的建議? (基本上,我有一個Map [Manifest [_],Blah],其中Blah處理基於類類型的case。Handling List [X]基於X可組合,但我需要能夠計算出X是什麼,所以我可以從地圖中獲取它的Blah值。)有沒有辦法從Scala中的Manifest [List [X]]中提取項目類型?
謝謝!
我知道什麼是List [_]基於清單我已經傳入一個方法,但我需要知道列表是什麼樣的項目。這些信息是否存儲在清單中的某個地方,並且可以將其清除?如果沒有,有關如何解決該問題的建議? (基本上,我有一個Map [Manifest [_],Blah],其中Blah處理基於類類型的case。Handling List [X]基於X可組合,但我需要能夠計算出X是什麼,所以我可以從地圖中獲取它的Blah值。)有沒有辦法從Scala中的Manifest [List [X]]中提取項目類型?
謝謝!
我認爲你正在尋找typeArguments
scala> manifest[List[Int]]
res1: Manifest[List[Int]] = scala.collection.immutable.List[Int]
scala> res1.typeArguments
res2: List[scala.reflect.Manifest[_]] = List(Int)
scala> res2.head
res3: scala.reflect.Manifest[_] = Int
scala> res3.erasure
res4: java.lang.Class[_] = int
很難告訴你該做什麼,沒有一段示例代碼。所以從你寫的東西我假設你得到一個A [B]作爲參數。這應該這樣工作:
def foo[A[B], B](x: A[B])(implicit outer: ClassManifest[A[B]], inner: ClassManifest[B]) = {
// your code here
}
所以,你有一個Manifest[List[T]]
和要處理的基礎上T
?如何
def listType[T](m: Manifest[T]) =
if (m.erasure == classOf[List[_]]) m.typeArguments match {
case List(c) if c.erasure == classOf[Int] => "it's a List[Int]"
case List(c) if c.erasure == classOf[String] => "it's a List[String]"
case _ => "some other List"
} else "not a List"
scala> listType(implicitly[Manifest[List[Int]]])
res29: java.lang.String = it's a List[Int]
這正是它。我知道必須有辦法進入那裏,但不知道如何去做。 – TOB 2012-03-12 15:41:59