2

我没有编译一个简单的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]      ^

我很感激解释这里出了什么问题。


解决方案:https ://stackoverflow.com/a/55137909/1862339

4

3 回答 3

3

原来问题是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)

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

于 2019-03-13T09:03:34.537 回答
2

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

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

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

这在Step 1自述文件中提到。

于 2019-03-12T18:36:31.063 回答
2

应该有一条错误消息,要求您启用paradise编译器插件。不幸的是,因为您使用的是 Scala 2.10,所以有一个错误会macro-compat阻止您看到错误消息。

所以长答案是:

  1. 通过scalaVersion in ThisBuild := "2.12.8"build.sbt.
  2. 您将看到一条错误消息,要求您添加paradise插件。
  3. build.sbt最后,根据错误消息 在您的设置中添加以下设置:
addCompilerPlugin("org.scalamacros" % "paradise" % "2.1.0" cross CrossVersion.full)
于 2019-03-13T03:06:33.697 回答