Kotlin apply function

This post was originally posted at https://agrawalsuneet.github.io/blogs/kotlin-apply-function/ and reposted on Medium on 29th April 2020.
  • let
  • with
  • run
  • apply
  • also
/**
* Calls the specified function [block] with `this` value as its receiver and returns `this` value.
*
*/
@kotlin.internal.InlineOnly
public inline fun <T> T.apply(block: T.() -> Unit): T {
contract {
callsInPlace(block, InvocationKind.EXACTLY_ONCE)
}
block()
return this
}
  1. The return type of the let function is nothing but the same calling object.
  2. Since its an extension function to the Template class, it can be called on any object.

Please continue reading at https://agrawalsuneet.github.io/blogs/kotlin-apply-function/

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store