与 Binding.scala 类型不匹配涉及 scala.xml.Elem

Type mismatch with Binding.scala involving scala.xml.Elem

我无法编译 Binding.scala 的简单示例,作为新手,我不知道如何修复它。也许自述文件有点过时了? https://github.com/ThoughtWorksInc/Binding.scala-sample 中的示例甚至更旧并且会导致弃用警告。

我的代码,基本上是从 README 中粘在一起的,甚至简化了一点:

import com.thoughtworks.binding.dom
import org.scalajs.dom.document
import scala.scalajs.js.annotation.JSExport

@JSExport
object SampleMain {

  @dom
  def table = {
    <table border="1" cellPadding="5">
      <thead>
        <tr>
          <th>Name</th>
          <th>E-mail</th>
        </tr>
      </thead>
      <tbody>
      </tbody>
    </table>
  }

  @JSExport
  def main(): Unit = {
    dom.render(document.body, table)
  }

}

fastOptJS导致编译错误:

SampleMain.scala:25:9: overloaded method value render with alternatives:
[error]   (parent: org.scalajs.dom.raw.Node,children: com.thoughtworks.binding.Binding[com.thoughtworks.binding.Binding.BindingSeq[org.scalajs.dom.raw.Node]],dummy: Unit)Unit <and>
[error]   (parent: org.scalajs.dom.raw.Node,children: com.thoughtworks.binding.Binding.BindingSeq[org.scalajs.dom.raw.Node])Unit <and>
[error]   (parent: org.scalajs.dom.raw.Node,child: com.thoughtworks.binding.Binding[org.scalajs.dom.raw.Node])Unit
[error]  cannot be applied to (org.scalajs.dom.raw.HTMLElement, scala.xml.Elem)
[error]     dom.render(document.body, table)
[error]         ^

我怀疑类型推断有问题并尝试了这个类型注释:def table: com.thoughtworks.binding.Binding[org.scalajs.dom.html.Table] 但这导致了另一个错误:

SampleMain.scala:11:6: type mismatch;
[error]  found   : scala.xml.Elem
[error]  required: com.thoughtworks.binding.Binding[org.scalajs.dom.html.Table]
[error]     (which expands to)  com.thoughtworks.binding.Binding[org.scalajs.dom.raw.HTMLTableElement]
[error]     <table border="1" cellPadding="5">
[error]      ^

如果能解释一下这里出了什么问题,我将不胜感激。


解决方案:

检查您是否已将此添加到您的 build.sbt

addCompilerPlugin("org.scalamacros" % "paradise" % "2.1.0" cross CrossVersion.full)

Binding.scala 的主要宏(将 Scala XML 文字翻译成特殊的 Binding 类型)在没有这个插件的情况下无法工作,编译器只能看到原始类型(scala.xml.Elem).

README 的 Step 1 中提到了这一点。

应该会出现错误信息,要求您启用paradise编译器插件。不幸的是,因为您使用的是 Scala 2.10,a bug in macro-compat 阻止您看到错误消息。

所以长答案是:

  1. 将 Scala 升级到 2.11 或 2.12,方法是在 build.sbt.
  2. 中设置类似 scalaVersion in ThisBuild := "2.12.8" 的内容
  3. 您将看到一条错误消息,要求您添加 paradise 插件。
  4. 最后,根据报错信息在你的build.sbt中添加如下设置:
addCompilerPlugin("org.scalamacros" % "paradise" % "2.1.0" cross CrossVersion.full)

原来问题是 paradise 编译器插件在我的案例中没有被选中。我正在构建一个仅在子项目中使用 Binding.scala 的 SBT 多项目,并且 addCompilerPlugin 不会传播到子项目。要使其工作,需要将其添加到子项目的设置中,如下所示:

lazy val client = (project in file("client"))
  .settings(
    libraryDependencies ++= Seq(
      "com.thoughtworks.binding" %%% "dom" % "11.6.0"
    ),
    addCompilerPlugin("org.scalamacros" % "paradise" % "2.1.0" cross CrossVersion.full)
  )
  .enablePlugins(ScalaJSPlugin)

之前我在 build.sbt 的顶层有 addCompilerPlugin,它不起作用并导致编译错误。