So I’m trying to learn how to use macros and I’m unable to get the “basic” example from the website to compile
Following : Macro Annotations | Macros | Scala Documentation
I have a file that looks like this
import scala.annotation.{StaticAnnotation, compileTimeOnly}
import scala.language.experimental.macros
import scala.reflect.macros.whitebox
// I commented this out to make sure I was overriding the message
//@compileTimeOnly("enable macro paradise to expand macro annotations")
class identity extends StaticAnnotation {
def macroTransform(annottees: Any*): Any = macro identityMacro.impl
}
object identityMacro {
def impl(c: whitebox.Context)(annottees: c.Expr[Any]*): c.Expr[Any] = {
import c.universe._
val inputs = annottees.map(_.tree).toList
val (annottee, expandees) = inputs match {
case (param: ValDef) :: (rest @ (_ :: _)) => (param, rest)
case (param: TypeDef) :: (rest @ (_ :: _)) => (param, rest)
case _ => (EmptyTree, inputs)
}
println((annottee, expandees))
val outputs = expandees
c.Expr[Any](Block(outputs, Literal(Constant(()))))
}
}
@identity
class Foo {}
but when I run it with what I believe is the right flag I get back an error saying that I’m missing the flag:
λ ~/Sandbox/scalamacros scalac -version
Scala compiler version 2.13.8 -- Copyright 2002-2021, LAMP/EPFL and Lightbend, Inc.
λ ~/Sandbox/scalamacros scalac -Ymacro-annotations atest.scala
atest.scala:26: error: macro annotation could not be expanded (since these are experimental, you must enable them with -Ymacro-annotations)
class Foo {}
^
1 error
scalac
came from coursier if that makes a difference.
I feel like I’m missing something super obvious, has anyone else encountered this?
ps: If this is the wrong place for this question, could someone point me to the correct place? I came here from github.com/scala/bug contribution guide