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

时间:2019-03-12 11:38:08

标签: sbt scala.js binding.scala

我无法编译 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

1 个答案:

答案 0 :(得分: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之前,它不起作用并导致了编译错误。