Skip to content

Instantly share code, notes, and snippets.

@milessabin
Created December 19, 2014 07:58
Show Gist options
  • Save milessabin/081dcf2807f9563052ce to your computer and use it in GitHub Desktop.
Save milessabin/081dcf2807f9563052ce to your computer and use it in GitHub Desktop.
whitebox/blackbox abort behaviour difference.
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