Skip to content

Instantly share code, notes, and snippets.

@som-snytt
Created May 9, 2020 01:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save som-snytt/b0a447ccc4555a4947c0eab33948db39 to your computer and use it in GitHub Desktop.
Save som-snytt/b0a447ccc4555a4947c0eab33948db39 to your computer and use it in GitHub Desktop.
post typer unit of work
package p
class Client {
def f = m.M.report("hello, world")
}
package m
import language.experimental.macros
import scala.reflect.macros.blackbox.Context
import scala.tools.nsc.Global
object M {
def report(s: String): Unit = macro Report.impl
}
object Report {
def impl(c: Context)(s: c.Expr[String]): c.Expr[Unit] = {
import c.universe._
val global = c.universe.asInstanceOf[Global]
s.tree match {
case Literal(Constant(text: String)) =>
c.enclosingUnit.asInstanceOf[global.CompilationUnit].toCheck += (() => println(s"*** $text ***"))
case t => c.abort(t.pos, "Not a literal")
}
c.Expr(Literal(Constant(())))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment