gpt4 book ai didi

斯卡拉宏 : How to get a Type object of a generic List

转载 作者:行者123 更新时间:2023-12-02 04:39:34 25 4
gpt4 key购买 nike

我正在处理一个 Scala 宏,我想将 Symbol 的类型与 List[T] 匹配,其中 T 是给定的 Type。我已经有了 T 类型参数的 Type 对象。

获取List[_]的Type对象很简单:

  val listType = c.weakTypeOf[List[_]]

有了这个我已经可以检查 typeSignature

  sourceParam.typeSignature match {
case paramType if paramType <:< listType =>
....
}

但我想用 List[T] 检查 paramType,所以我如何在采用 List[_] 和一些 Type 的地方创建一个 Type 对象并使其成为新类型?

最佳答案

scala> val tpeList = typeOf[List[_]]
tpeList: reflect.runtime.universe.Type = scala.List[_]

scala> appliedType(tpeList, List(typeOf[Int]))
res0: reflect.runtime.universe.Type = scala.List[Int]

关于斯卡拉宏 : How to get a Type object of a generic List,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21164811/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com