Advertisement
Guest User

Untitled

a guest
Dec 19th, 2014
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.60 KB | None | 0 0
  1. import scala.language.experimental.macros
  2.  
  3. import scala.reflect.macros.whitebox
  4.  
  5. trait Foo[T]
  6.  
  7. object Foo {
  8. implicit def mkFoo[T]: Foo[T] = macro FooMacros.mkFooImpl[T]
  9. }
  10.  
  11. // with whitebox.Context the abort message is lost
  12. // with blackbox.Context the abort message is preserved
  13. class FooMacros(val c: whitebox.Context) {
  14. import c.universe._
  15.  
  16. def mkFooImpl[T](implicit tTag: WeakTypeTag[T]): Tree = {
  17. val tTpe = weakTypeOf[T]
  18. val t = c.inferImplicitValue(tTpe, silent = true)
  19. if(t == EmptyTree)
  20. c.abort(c.enclosingPosition, "Custom missing implicit")
  21.  
  22. q""" new Foo[$tTpe] """
  23. }
  24. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement