Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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] """
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement