[英]How to check if WeakTypeTag or Type represents concrete type?
How do I check if WeakTypeTag
or Type
represents concrete type? 如何检查
WeakTypeTag
或Type
表示具体类型? This would be especially useful in macros, where I could use it to raise compilation error when type given by the user is not concrete: 这在宏中特别有用,在用户给出的类型不具体时,我可以使用它来引发编译错误:
def macroMethod[T]: Unit = macro macroMethod_impl[T]
def macroMethod_impl[T: c.WeakTypeTag](c: Context): c.Expr[Unit] = {
import c.universe._
def isConcrete(tpe: Type) = ???
if(!isConcrete(weakTypeOf[T])) {
c.error(c.enclosingPosition, "You must provide concrete type.")
}
c.literalUnit
}
I think this will do the trick: 我认为这样可以解决问题:
def isConcrete(tpe: Type) = !tpe.typeSymbol.asType.isAbstractType
then 然后
scala> macroMethod[Int]
scala> class C[T] { macroMethod[T] }
<console>:10: error: You must provide concrete type.
class C[T] { macroMethod[T] }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.