Scala 中带有接收者的函数类型

Function type with receiver in Scala

我正在研究这个 Kotlin 示例:

class HTML {
    fun body() { ... }
}

fun html(init: HTML.() -> Unit): HTML {
  val html = HTML()  // create the receiver object
  html.init()        // pass the receiver object to the lambda
  return html
}


html {       // lambda with receiver begins here
    body()   // calling a method on the receiver object
}

我想知道如何在 Scala 中编写这段代码?如何在带有接收器的scala函数类型中声明?

Scala 中没有与此等效的方法。您只需使用一个将 HTML 作为参数的函数(可能作为隐式参数,但这不会反映在类型中,在这种情况下不太可能)。