Hello
I am new to Kotlin, learning lambdas.
In the manual there is this example:
class HTML {
fun body() { println (" inside 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 want to pass one input parameter to the fun body().
Here is my code, which works correctly: (please ignore the formatting)
class HTML {
fun body(s : String ) { println ("body() $s") }
}
fun html( s : String = "blank", init: HTML.(String) -> Unit) : HTML {
val h = HTML() // create the receiver object
h.init(s) // pass the receiver object to the lambda, same as below
return h
}
html { body("yuri") }
Furthermore, passing html( “parameter”) explicitly, still will use the parameter value passed from the call to function body(“budilov”), not from html(“NO”) call.
html("NO") { body("budilov") }
So “budilov” is printed, not the “NO”
–
I dont know if this is the correct/recommended way of passing a parameter.
For a start, my solution requires input parameter to use default i.e. = “default value”, else it wont compile unless I pass the same parameter twice. This tells me that I am not using best Kotlin practice here.
Can you please comment on my solution and tell me the best-practice way of doing so?
All of the above print the same output, which is this: body() hello yuri
What I am trying to understand is this: how does the html(“hello)” becomes the value is $it or $z in the body of the { z → body(“$z yuri”) } - given they are part of the same call on one function, namely html() which takes 2 parameters - a string and a lambda.
what you write: html(“first”) { body(“second”) }
with replaced sugar (not all sugar, but enough) val second: HTML.(String) → Unit = { string → body(“second”) } html(“first”, second)
what you want: val second: HTML.(String) → Unit = { s → body(s + " second") } html(“first”, second)
or with sugar html(“first”){ body(it + " second") }
and this is actually pointless dsl, it can be useful then your HTML and BODY has separate fabric methods and HTML contains BODY typed property, and render method which print nodes
*you shouldn’t start lambdas with dsl-examples, it’s too complex
then you use functional parameter “init” you make extension for your fabric method “html” which allows you to configure created object with calls on the consumer side like:
class HTML {
private var _body = BODY()
fun body(init: BODY.() -> Unit) {
_body.init()
}
fun render() {
print("<html>")
_body.render()
print("</html>")
}
}
class BODY {
var text = ""
fun render() = print("<body>$text</body>")
}
fun html(init: HTML.()->Unit):HTML = HTML().apply{ init() }
// now we use it:
html {
body {
text = "body"
}
}.render()
Your first 2 variations are just a sugar code versions for the last line.
The last one should be easy to understand:
The parameter of “html(s)” get passed to “body()” by code “h.init(s)” in “html()” definition.