Kotlin let function

This post was originally posted at https://agrawalsuneet.github.io/blogs/kotlin-let-function/ and reposted on Medium on 26th Feb 2020.
  • let
  • with
  • run
  • apply
  • also
/**
* Calls the specified function [block]
* with `this` value as its argument
* and returns its result.
*
*/
@kotlin.internal.InlineOnly
public inline fun <T, R> T.let(block: (T) -> R): R {
contract {
callsInPlace(block, InvocationKind.EXACTLY_ONCE)
}
return block(this)
}
  1. The return type of the let function is nothing but the last expression we returned from our passed lambda parameter.
  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-let-function/

--

--

--

https://agrawalsuneet.github.io/

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

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

Fused Location Provider in Kotlin

Android Protobuf with kotlin and Wire

Kotlin Cookbook 1

RetryWithTimes — A helper function build with coroutine