有没有办法从 Scala 的 Manifest[List[X]] 中提取项目类型? [英] Is there a way to extract the item type from a Manifest[List[X]] in Scala?

查看:53
本文介绍了有没有办法从 Scala 的 Manifest[List[X]] 中提取项目类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道某些东西是基于我传递给方法的 Manifest 的 List[_],但我需要知道列表是什么类型的项目.该信息是否存储在 Manifest 中的某个地方,您可以将其取出吗?如果没有,有关如何解决此问题的任何建议?

I know something is a List[_] based on the Manifest I've passed into a method, but I need to know what kind of item the list is. Is that information stored in the Manifest somewhere and can you get it out? If not, any suggestions about how to work around the issue?

(基本上,我有一个 Map[Manifest[_], Blah],其中 Blah 根据类类型处理案例.处理 List[X] 是基于 X 的可组合的,但我需要能够弄清楚什么X 是这样我可以从地图中获取它的 Blah 值.)

(Basically, I have a Map[Manifest[_], Blah], where Blah handles cases based on the class type. Handling List[X] is composable based on X, but I need to be able to figure out what X is so I can grab its Blah value out of the map.)

谢谢!

推荐答案

我认为您正在寻找 typeArguments

I think you're looking for 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

这篇关于有没有办法从 Scala 的 Manifest[List[X]] 中提取项目类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆