【问题标题】:What is the type of quasiquote that matched type parameters?匹配类型参数的准引用类型是什么?
【发布时间】:2013-09-15 15:33:26
【问题描述】:

我有一个 quoasiquote matcher where q"someMethod[$ts]()" where def someMethod[I <: shapeless.HList]()"

打印ts 给出:

List(shapeless.HNil)

或者,例如

List(String, Int)

然后我尝试分配:

val types: List[scala.reflect.api.Types.Type] = ts

导致编译器错误:

[error] type mismatch;
[error]  found   : List[_125.u.Tree] where val _125: scala.reflect.api.QuasiquoteCompat.TypeAppliedExtractor{val u: OpTreeContext.this.c.universe.type}
[error]  required: List[Context.this.c.universe.Type]

如何从ts 合成List[scala.reflect.api.Types.Type]

【问题讨论】:

    标签: scala scala-macros shapeless scala-quasiquotes


    【解决方案1】:

    正如您的错误消息所述,ts 是一个树列表。更具体地说,这些将是type trees,您可以使用tpe 方法提取它们的类型:

    val types: List[scala.reflect.api.Types#Type] = ts.map(_.tpe)
    

    请注意,我使用的是类型投影,因为Types 既不是包也不是对象,所以Types.Type 不起作用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-01-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-27
      • 2016-12-12
      • 1970-01-01
      相关资源
      最近更新 更多