Created
December 19, 2014 07:58
-
-
Save milessabin/081dcf2807f9563052ce to your computer and use it in GitHub Desktop.
whitebox/blackbox abort behaviour difference.
This file contains hidden or 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
import scala.language.experimental.macros | |
import scala.reflect.macros.whitebox | |
trait Foo[T] | |
object Foo { | |
implicit def mkFoo[T]: Foo[T] = macro FooMacros.mkFooImpl[T] | |
} | |
// with whitebox.Context the abort message is lost | |
// with blackbox.Context the abort message is preserved | |
class FooMacros(val c: whitebox.Context) { | |
import c.universe._ | |
def mkFooImpl[T](implicit tTag: WeakTypeTag[T]): Tree = { | |
val tTpe = weakTypeOf[T] | |
val t = c.inferImplicitValue(tTpe, silent = true) | |
if(t == EmptyTree) | |
c.abort(c.enclosingPosition, "Custom missing implicit") | |
q""" new Foo[$tTpe] """ | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment