Advertisement
Guest User

Untitled

a guest
Jun 18th, 2019
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.73 KB | None | 0 0
  1. import scala.language.experimental.macros
  2. import scala.reflect.macros.whitebox
  3.  
  4.  
  5.  
  6. private[foo] class Macros(val c: whitebox.Context) {
  7. import c.universe._
  8. def xyzLiteral(s1: c.Expr[String], s2: c.Expr[String]): Tree =
  9. (s1.tree, s2.tree) match {
  10. case (Literal(Constant(s1: String)), Literal(Constant(s2: String))) =>
  11. if (s1 == s2) c.abort(c.enclosingPosition, "Strings Match")
  12. else q"_root_.constructor.method($s1, $s2)"
  13. case _ =>
  14. c.abort(
  15. c.enclosingPosition,
  16. s"This method uses a macro to verify all strings are unique, use XYZ to get dynamic behavior"
  17. )
  18. }
  19. }
  20.  
  21. def apply(s1: String, s2: String): constructedType = macro Macros.xyzLiteral
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement