I have a problem to use the official code to try some reflection [https://docs.scala-lang.org/overviews/reflection/environment-universes-mirrors.html]
I used sbt to create 2 packages, the app package depends on the macro package, I put the macro code in the macro package and import the code to app package to call it, but it was not working well.
import scala.reflect.macros.Context
case class Location(filename: String, line: Int, column: Int)
object Macros {
def currentLocation: Location = macro impl
def impl(c: Context): c.Expr[Location] = {
import c.universe._
val pos = c.macroApplication.pos
val clsLocation = c.mirror.staticModule("Location") // get symbol of "Location" object
c.Expr(Apply(Ident(clsLocation), List(Literal(Constant(pos.source.path)), Literal(Constant(pos.line)), Literal(Constant(pos.column)))))
}
}
[error] scala.ScalaReflectionException: object Location not found. [error] at scala.reflect.internal.Mirrors$RootsBase.staticModule(Mirrors.scala:168) [error] at scala.reflect.internal.Mirrors$RootsBase.staticModule(Mirrors.scala:29) [error] at sg.bigo.Macros$.impl(Macros.scala:61) [error] currentLocation.column
