我正在尝试在我的项目中包含宏注释。按照文档,我尝试实现他们的示例。
我知道宏模块必须在核心模块之前编译(核心是包含使用宏注释的代码的模块(。为此,我创建了以下build.sbt(版本1.2.8(:
name := "test"
lazy val commonSettings = Seq(
version := "0.1",
scalaVersion := "2.12.8"
)
lazy val macros = (project in file("macros")).settings(
commonSettings,
libraryDependencies += "org.scala-lang" % "scala-reflect" % scalaVersion.value
)
lazy val core = (project in file("core")).settings(
commonSettings
) dependsOn macros
我的项目结构如下:
+-- .idea
+-- core
| +-- src
| | +-- java
| | +-- scala
| | | +-- Test.scala
+-- macros
| +-- src
| | +-- java
| | +-- scala
| | | +-- identity.scala
...
但是,当我在 Test 类中使用 @identity
注释时,我仍然收到宏注释未展开的消息(由于@compileTimeOnly("enable macro paradise to expand macro annotations")
(。有什么想法吗?
添加
addCompilerPlugin("org.scalamacros" % "paradise" % "2.1.1" cross CrossVersion.full)
到commonSettings
(Scala 2.11-2.12(。
在 Scala 2.13 中,打开scalacOptions += "-Ymacro-annotations"
就足够
在 Scala 中自动生成案例类的伴随对象