SBT/Scala: 未找到宏实现

SBT/Scala: macro implementation not found

我尝试了宏,但一直运行进入错误

macro implementation not found: W [error] (the most common reason for that is that you cannot use macro implementations in the same compilation run that defines them)

相信我设置了一个两遍编译,首先编译宏实现,然后再编译用法。

这里是/build.sbt的一部分:

lazy val root = (project in file(".")).
  settings(rootSettings: _*).
  settings(name := "Example").
  aggregate(macros, core).
  dependsOn(macros, core)

lazy val macros = (project in file("src/main/com/example/macros")).
  settings(macrosSettings: _*).
  settings(name := "Macros")

lazy val core = (project in file("src/main/com/example/core")).
  settings(coreSettings: _*).
  settings (name := "Core").
  dependsOn(macros)


lazy val commonSettings = Seq(
  organization := Organization,
  version := Version,
  scalaVersion := ScalaVersion
)

lazy val rootSettings = commonSettings ++ Seq(
  libraryDependencies ++= commonDeps ++ rootDeps ++ macrosDeps ++ coreDeps
)

lazy val macrosSettings = commonSettings ++ Seq(
  libraryDependencies ++= commonDeps ++ macrosDeps
)

lazy val coreSettings = commonSettings ++ Seq(
  libraryDependencies ++= commonDeps ++ coreDeps
)

宏实现如下所示:

/src/main/com/example/macros/Macros.scala

object Macros {
  object Color {
    def ColorWhite(c: Context): c.Expr[ObjectColor] = c.Expr[ObjectColor](c.universe.reify(ObjectColor(White())).tree)
  }
}

用法如下:

/src/main/com/example/core/Main.scala

object Macros {
  import com.example.macros.Macros._
  def W: ObjectColor = macro Color.ColorWhite
}

object Main extends App {
  import Macros._
  println(W)
}

Scala 2.11.6。 SBT 0.13.8.

我做错了什么?

感谢您的建议!

错误项目:

The Project on Github

工作项目:

将项目重新排列为更正确的形式:

The cleanedup working project

您的 macroscore 项目不包含任何文件,因此它们不会导致问题。错误发生在 sbt 编译 root 时,由于你在 sbt build.

中说 project in file(".") 而包含 Main.scala 和 Macros.scala