scala - Scala中带有接收器的函数类型

标签 scala lambda kotlin

我正在研究这个 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 作为参数的函数(可能作为隐式参数,但这不会反射(reflect)在类型中并且在这种情况下不太可能)。

关于scala - Scala中带有接收器的函数类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35083457/

相关文章:

scala - 为什么允许我将返回非 Unit 的函数传递给采用返回 Unit 的函数的方法?

java - 如何将类放入 Scala 中 JJS ScriptEngine 的范围中

scala - 更改在端口 7077 上运行的绑定(bind) IP - Apache Spark

java - 热至 "flatten"TornadoFX(JavaFX 扩展)中 ObjectProperty 的 ObjectProperty?

java - 不幸的是MyApp已停止。我该如何解决?

Scala - 嵌套循环、for 理解和最终迭代的类型

silverlight - 是否可以绑定(bind)到 Silverlight 中的 lambda 表达式?

java - 如何使用java查找shell脚本中的所有交互式命令?

python - "What does this mean: key=lambda x: x[-1] ?"

java - Android 的 Kotlin "expensive"中的枚举类是否与 Java 中的一样?