加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

Scala中带接收器的函数类型

发布时间:2020-12-16 18:56:30 所属栏目:安全 来源:网络整理
导读:我正在调查这个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 rec
我正在调查这个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作为参数的函数(可能作为隐式参数,但这并未反映在类型中,在这种情况下不太可能).

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读