【问题标题】:Function type with receiver in ScalaScala中带有接收器的函数类型
【发布时间】:2016-01-29 11:30:47
【问题描述】:

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


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-03-03
      • 2019-03-13
      • 2014-10-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多