SBT / Scala:未找到宏实现

时间:2015-04-27 09:25:58

标签: scala macros sbt

我尝试了我的手,我一直遇到错误

  

未找到宏实现:W   [错误](最常见的原因是你不能在定义它们的同一个编译运行中使用宏实现)

相信我已经设置了一个双向编译,首先编译宏实现,然后使用第二个。

这是/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。

我做错了什么?

感谢您的建议!

Fawlty项目:

The Project on Github

工作项目:

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

The cleanedup working project

1 个答案:

答案 0 :(得分:3)

您的macroscore项目不包含任何文件,因此不会导致问题。当sbt编译root时会发生错误,它包含Main.scala和Macros.scala,因为你在sbt构建中说project in file(".")