Skip to content

Instantly share code, notes, and snippets.

@4e6
Created April 30, 2013 11:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 4e6/5488205 to your computer and use it in GitHub Desktop.
Save 4e6/5488205 to your computer and use it in GitHub Desktop.
Check if type is concrete
def f[T]: Unit = macro f_impl[T]
def f_impl[T: c.WeakTypeTag](c: Context): c.Expr[Unit] = {
import c.universe._
def isConcreteTypeSymbol(sym: Symbol) =
sym.isType && !sym.asType.isAbstractType
def isConcreteType(tpe: Type): Boolean = tpe match {
case NoPrefix =>
println("NoPrefix")
false
case TypeRef(pre, sym, args) =>
println(s"TypeRef($pre, $sym, $args)")
isConcreteType(pre) &&
isConcreteTypeSymbol(sym) &&
args.forall(isConcreteType)
case ExistentialType(quantified, underlying) =>
println(s"ExistentialType($quantified, $underlying)")
false
case _ =>
println(tpe)
isConcreteTypeSymbol(tpe.typeSymbol)
}
if(!isConcreteType(weakTypeOf[T])) {
c.error(c.enclosingPosition, "You must provide concrete type.")
} else {
c.echo(c.enclosingPosition, "Ok")
}
c.literalUnit
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment