我在使用官方代码尝试反思时遇到问题[https://docs.scala-lang.org/overviews/reflection/environment-universes-mirrors.html]
我使用sbt创建了2个程序包,应用程序包取决于宏程序包,我将宏代码放入宏程序包中,然后将代码导入到应用程序包中以进行调用,但是效果不佳。
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)))))
}
}
[错误] scala.ScalaReflectionException:对象找不到位置。 [错误]在scala.reflect.internal.Mirrors $ RootsBase.staticModule(Mirrors.scala:168) 在scala.reflect.internal.Mirrors $ RootsBase.staticModule(Mirrors.scala:29)处出现[错误] sg.bigo.Macros $ .impl(Macros.scala:61)出现[错误] [错误] currentLocation.column
答案 0 :(得分:1)
以下项目编译
App.scala
import Macros.currentLocation
object App {
def main(args: Array[String]): Unit = {
println(
currentLocation //Location(/media/data/Projects/macrosdemo213/core/src/main/scala/App.scala,6,7)
)
}
}
Macros.scala
import scala.language.experimental.macros
import scala.reflect.macros.blackbox
case class Location(filename: String, line: Int, column: Int)
object Macros {
def currentLocation: Location = macro impl
def impl(c: blackbox.Context): c.Tree = {
import c.universe._
val pos = c.macroApplication.pos
val clsLocation = c.mirror.staticModule("Location")
q"$clsLocation(${pos.source.path}, ${pos.line}, ${pos.column})"
}
}
build.sbt
name := "macrosdemo213"
lazy val commonSettings = Seq(
scalaVersion := "2.13.0",
organization := "com.example",
version := "1.0.0",
)
lazy val macros: Project = (project in file("macros")).settings(
commonSettings,
libraryDependencies ++= Seq(
scalaOrganization.value % "scala-reflect" % scalaVersion.value,
)
)
lazy val core: Project = (project in file("core")).aggregate(macros).dependsOn(macros).settings(
commonSettings
)
build.properties
sbt.version = 1.2.8
项目结构