scala – 如何在模式匹配中获得清单
发布时间:2020-12-16 09:06:02 所属栏目:安全 来源:网络整理
导读:我想得到一个List的内部类型的清单,如下所示并将其传递给另一个函数,我该怎么做?谢谢 def f(any: Any) = any match { case x: Int = println("Int") case a: List[_] = // get the manifest of List's inner type,and use it in the function g() } def g[T
我想得到一个List的内部类型的清单,如下所示并将其传递给另一个函数,我该怎么做?谢谢
def f(any: Any) = any match { case x: Int => println("Int") case a: List[_] => // get the manifest of List's inner type,and use it in the function g() } def g[T:Manifest](list:List[T]) = {} 解决方法
将清单添加为方法的隐式需求,并稍微调整类型签名:
def f[T](any: T)(implicit mf: Manifest[T]) = mf match { case m if m == Manifest[Int] => println("Int") case m if m == Manifest[List[Int]] => println("List of Ints") //etc... } Manifest类有一个方法typeArguments,它应该用于查找“内部类型”的目的.例如 manifest[List[Int]].typeArguments == List(manifest[Int]) (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |