Следующий проект компилируется
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
Структура проекта