How to check if WeakTypeTag or Type is a specific type?

How to check if WeakTypeTag or Type WeakTypeTag specific type? This would be especially useful in macros, where I could use it to raise a compilation error when the type specified by the user is not specific:

 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 } 
+4
source share
1 answer

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] } 
+1
source

All Articles