Apply

This function extends the generic T parameter with no constraints, which means that any Type declared in Kotlin can use it. It accepts a receiver function, and the receiver is the instance on which the apply function is called and then returns the same instance. Since you can access the instance of the object inside the apply function, you can use it for creating objects and initializing them in the same place:

var stringBuilder = StringBuilder().apply {
appendln("I'm the first line of the String Builder")
}

Here we created an instance of the StringBuilder class and used the apply function to append the first line.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.138.181.196