【问题标题】:Scala 2.13.1 Unable to use macrosScala 2.13.1 无法使用宏
【发布时间】:2020-02-14 07:27:13
【问题描述】:

我无法在 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 macros


    【解决方案1】:

    它们不在标准库中,而是在单独的scala-reflect 中,需要在build.sbt 中作为依赖添加:

    libraryDependencies += "org.scala-lang" % "scala-reflect" % scalaVersion.value
    

    您可以从Context documentation 看到左上角的“Scala 反射库”。

    【讨论】:

    • 这是在哪里记录的?我在官方文档中找不到。
    • 这就是我添加最后一行的原因。
    猜你喜欢
    • 1970-01-01
    • 2012-02-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-12
    • 2012-07-25
    相关资源
    最近更新 更多