Search code examples
scalacompilationmacrosannotationssbt

Scala macro annotations do not get expanded (macro paradise)


I'm trying to include macro annotations in my project. Following the documentation, I tried implementing their example.

I am aware that the macro module has to be compiled before the core module (core being the module that contains the code that utilises the macro annotations). To achieve this I created the following build.sbt (version 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

The structure of my project is the following:

+-- .idea
+-- core
|   +-- src
|   |   +-- java
|   |   +-- scala
|   |   |   +-- Test.scala
+-- macros
|   +-- src
|   |   +-- java
|   |   +-- scala
|   |   |   +-- identity.scala
...

However, when I use the @identity annotation in the Test class, I still get the message that the macro annotation is not expanded (due to the @compileTimeOnly("enable macro paradise to expand macro annotations")). Any ideas on why?


Solution

  • Add

    addCompilerPlugin("org.scalamacros" % "paradise" % "2.1.1" cross CrossVersion.full)
    

    to commonSettings (Scala 2.11-2.12).

    In Scala 2.13 it's enough to switch on scalacOptions += "-Ymacro-annotations"

    Auto-Generate Companion Object for Case Class in Scala