我正在研究这个 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/