简体   繁体   English

在Scala宏中匹配通用类型

[英]Matching generic types in Scala macros

I have a Scala (2.10) def macro with argument x . 我有一个带有参数x的Scala(2.10)def宏。 It's body looks like this: 它的身体看起来像这样:

if (x.actualType =:= typeOf[generic.Type[String]]) {
  // branch 1: do something with x
}
else {
  // branch 2: do something else
}

The implementation of the generic.Type class itself is not accessible and its implementation cannot be modified. generic.Type类本身的实现不可访问,并且其实现无法修改。 Now, I'd like this macro to call branch 1 not just for generic.Type[String] s, but for generic.Type[T] , where T is any type. 现在,我希望该宏不仅为generic.Type [String]调用分支1,而且还为generic.Type [T]调用分支,其中T是任何类型。

Is there a canonical way to achieve that? 有没有实现这一目标的规范方法? Or is the only option doing it manually using the AST? 还是使用AST手动执行此操作的唯一选择?

尝试x.actualType.typeSymbol == typeOf[generic.Type[_]].typeSymbol

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM