Sol
Sol

Reputation: 320

Function type with receiver in Scala

I'm investigating this Kotlin example:

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
}

I'm wonder how to write this code in scala? How to declare in scala function type with receiver?

Upvotes: 7

Views: 725

Answers (1)

Alexey Romanov
Alexey Romanov

Reputation: 170745

There is no equivalent to this in Scala. You'd simply use a function which takes HTML as an argument (possibly as an implicit argument, but this isn't reflected in the type and unlikely in this case).

Upvotes: 7

Related Questions