scala - Scala 2.13.1 无法使用宏
问题描述
我无法在 scala中使用宏2.13.1
。
我不断收到以下错误:
object blackbox is not a member of package scala.reflect.macros
这显然不是真的,因为 scala 2.13.1 实际上有这些黑盒。我做错了什么?
你好.scala
package example.core
import example.macros.MacroLibrary
object Hello extends App {
MacroLibrary.hello()
}
宏库.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
}
构建.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
您可以从左上角有“Scala Reflection Library”的Context
文档中看到。
推荐阅读
- push-notification - 从 OneSignal 迁移到 FoxPush 推送通知
- google-apps-script - 如何在来自附加侧边栏的自定义函数中插入数据
- checkbox - 将复选框重新设置为 true(处理空白)- Google Apps 脚本
- java - 使用 spock lang 规范测试控制器的选项
- java - Float to Int vs Int to Float 自动转换
- c# - SMART SSD 信息 C#
- marklogic - MarkLogic - 摄取和协调性能调整
- javascript - 如何遍历 Django 模板中的字典?
- javascript - javascript提示不显示
- javascript - CORS 策略已阻止从来源 *** 获取 *** 的访问权限:无“访问控制允许来源”