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/

--

--

--

https://agrawalsuneet.github.io/

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

How to Create an S3 Bucket

A yellow bucket on a sandy beach

The Ultimate Algorithm for the Most Awesomes Life v.2021–03

CS373 Fall 2021 Entry 13: Justin Lee

Absolutely Everything From Before, Now With Bluetooth

My journey into the shoes of a coder|Learning update: Moment of enlightenment

What is hot about SD-WAN ?what are the product features? What’s the requirement for deployment?

My Insightful Experience with Crio’s #IBelieveInDoing OOPs workshop

C++ Unordered Map Under the Hood

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
Suneet Agrawal

Suneet Agrawal

https://agrawalsuneet.github.io/

More from Medium

Kotlin Functions

Kotlin Programming Language: Everything You Need to Know

Kotlin Collections & Operations

TDD — Simple Demonstration in Kotlin