Scala 2.13.1 无法使用宏
Scala 2.13.1 Unable to use macros
我无法在 Scala 2.13.1
.
中使用 宏
我不断收到以下错误:
object blackbox is not a member of package scala.reflect.macros
这显然不是真的,因为 scala 2.13.1 实际上有这些黑盒。
我做错了什么?
Hello.scala
package example.core
import example.macros.MacroLibrary
object Hello extends App {
MacroLibrary.hello()
}
MacroLibrary.scala
package example.macros
// This line throws errors
import scala.reflect.macros.blackbox.Context
import scala.reflect.macros.blackbox
object MacroLibrary {
def hello_impl(c: blackbox.Context)(): c.Expr[Unit] = {
import c.universe._
c.Expr(q"""println("Hello World")""")
}
def hello(): Unit = macro hello_impl
}
build.sbt
scalaVersion := "2.13.1"
错误
object blackbox is not a member of package scala.reflect.macros
它们不在标准库中,而是在单独的scala-reflect
中,需要在build.sbt
中添加为依赖:
libraryDependencies += "org.scala-lang" % "scala-reflect" % scalaVersion.value
您可以从 Context
documentation 看到左上角有 "Scala Reflection Library"。
我无法在 Scala 2.13.1
.
我不断收到以下错误:
object blackbox is not a member of package scala.reflect.macros
这显然不是真的,因为 scala 2.13.1 实际上有这些黑盒。 我做错了什么?
Hello.scala
package example.core
import example.macros.MacroLibrary
object Hello extends App {
MacroLibrary.hello()
}
MacroLibrary.scala
package example.macros
// This line throws errors
import scala.reflect.macros.blackbox.Context
import scala.reflect.macros.blackbox
object MacroLibrary {
def hello_impl(c: blackbox.Context)(): c.Expr[Unit] = {
import c.universe._
c.Expr(q"""println("Hello World")""")
}
def hello(): Unit = macro hello_impl
}
build.sbt
scalaVersion := "2.13.1"
错误
object blackbox is not a member of package scala.reflect.macros
它们不在标准库中,而是在单独的scala-reflect
中,需要在build.sbt
中添加为依赖:
libraryDependencies += "org.scala-lang" % "scala-reflect" % scalaVersion.value
您可以从 Context
documentation 看到左上角有 "Scala Reflection Library"。