Created
April 30, 2013 11:46
-
-
Save 4e6/5488205 to your computer and use it in GitHub Desktop.
Check if type is concrete
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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