When I use Binding.scala, I got the error `each instructions must be inside a SDE block`, how can I fix this?

∥☆過路亽.° 提交于 2019-12-24 00:18:39

问题


When I use Binding.scala, I want to create some divs according to source data someCollection:

val someCollection = Seq("foo", "bar")
someCollection.map { item =>
  <div>{item.bind}</div>
}

However, I got a compiler error each instructions must be inside a SDE block.

How can I fix this?


回答1:


The code that causes this error is that your bind expression must not be outside of the scope of the @dom macro. This can happen when creating a closure and can be resolved by:

  1. Refactoring the code in the closure into its own @dom annotated method.
  2. Converting someCollection to a BindingSeq, for example:

    Constants(someCollection: _*).map { item => <div>{item.bind}</div> }

  3. Provide a scalaz.Traverse type class for the collection (Run this example on ScalaFiddle)

TL;DR

@dom def renderList(data: List[Binding[String]]) = <ol>{
  import scalaz.std.list._ // Type classes for List
  for (b <- data) yield {
    <li>{b.bind}</li>
  }
}</ol>


来源:https://stackoverflow.com/questions/42498968/when-i-use-binding-scala-i-got-the-error-each-instructions-must-be-inside-a-sd

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!