巨集註釋

這個簡單的巨集註釋按原樣輸出帶註釋的項。

import scala.annotation.{compileTimeOnly, StaticAnnotation}
import scala.reflect.macros.whitebox.Context

@compileTimeOnly("enable macro paradise to expand macro annotations")
class noop extends StaticAnnotation {
  def macroTransform(annottees: Any*): Any = macro linkMacro.impl
}

object linkMacro {
  def impl(c: Context)(annottees: c.Expr[Any]*): c.Expr[Any] = {
    import c.universe._

    c.Expr[Any](q"{..$annottees}")
  }
}

@compileTimeOnly 註釋生成錯誤,並顯示一條訊息,指示必須包含 paradise 編譯器外掛才能使用此巨集。通過 SBT包含此說明的說明在這裡

你可以像這樣使用上面定義的巨集:

@noop
case class Foo(a: String, b: Int)

@noop
object Bar {
  def f(): String = "hello"
}

@noop
def g(): Int = 10