Skip to content

Instantly share code, notes, and snippets.

@arjunguha
Last active August 29, 2015 14:01
Show Gist options
  • Save arjunguha/723389cce08226a3e759 to your computer and use it in GitHub Desktop.
Save arjunguha/723389cce08226a3e759 to your computer and use it in GitHub Desktop.
Yrangepos error
scalaVersion in ThisBuild := "2.11.1"
scalacOptions in ThisBuild += "-Yrangepos"
libraryDependencies in ThisBuild += "org.scala-lang" % "scala-reflect" % "2.11.1"
lazy val root = project.aggregate(client, macro)
lazy val macro = project
lazy val client = project.dependsOn(macro)
// Place in client subdirectory
import Macro._
object Main extends App {
mymacro {
println("Printed range")
println("Should span several characters, but does not :(")
}
}
// Place in macro subdirectory
import scala.language.experimental.macros
import scala.reflect.macros.blackbox.Context
object Macro {
def macroImpl(c: Context)(body : c.Expr[Unit]) : c.Expr[Unit] = {
import c.universe._
val p = body.tree.pos
val str = s"Line: ${p.line}. Character range: ${p.start} --- ${p.end}"
val loc = c.Expr[String](Literal(Constant(str)))
reify {
println(loc.splice)
body.splice
}
}
def mymacro(body : Unit) : Unit = macro macroImpl
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment