Exploring Easter functions in Kotlin

Akshay Chordiya
Published in
3 min readJul 7, 2017


Every time I look at Kotlin, there is always something new which I find or a more sugary way for tedious way in Java. It’s like complete language is an easter egg, LOL!

Easter Eggs

Today with this article I intend to share these small but interesting and really useful functions.

let function

let is an extension function for all types of objects. simple scoping function. It lets you declare a variable for a given scope. It is mainly used for 2 purposes:

  • To make sure the block executes if the variable is not null
  • Keeps your code self — contained to prevent leaking and separation of logic
let function example

The let block is executed if the value of person is not null. It’s functionally similar to if block only difference being no else block.

with function

In Java, to set multiple properties for the object we write bunch of code or use Builder pattern.

Java setter example

with block uses variable as it’s context that means the context of the variable is available inside the block. Hence, no need to use variable name again for further stuff. It really helps to eliminate the repetitive code for setting the properties.

with function example

EDIT: By default with takes the function parameter and return the function result, which in this case it is inferred to Unit. To make with function to return a Person instance you need to return this in the block. Credits to Álvaro Blanco Cabrero for pointing out.

with function example with return

apply function

apply is an extension function for all types of object. Functionality — wise it is pretty much similar to with function.

apply function example

also function

also is like apply ; it takes the receiver, does some operation and returns the receiver. Added in Kotlin 1.1

The difference is inside the apply block the receiver is available as this in case of also it is available as it. It is handy when you don’t want to shadow this from outer scope.

also function example

You can even give custom name if you want.

also function with custom name example

use function

use function is equivalent of Java’s try — with — resources. It applies to all types of Closeable instances. It automatically closes the resource (receiver) on exit.

Java’s implementation:

Try with resource in Java

Kotlin style:

use function example

These kind of examples make me fall in love with Kotlin. It looks so beautiful!

run function

run is a pretty simple function. It’s functionally similar to with and let which makes it similar to apply . The only difference is run returns result as return unlike apply which returns it’s receiver.

takeIf function

takeIf is filter for single value. It checks if the receiver meets the criteria or predicate and returns the receiver if it does or null if it doesn’t. Added in Kotlin 1.1

It is equivalent to if(predicate(it)) it else null. Combined with ternary operator you can handle the else condition.

takeIf function example

The following example returns index if it matches the predicate else returns 0

takeUnless function

It’s exactly opposite to takeIf . It takes an inverted predicate. Added in Kotlin 1.1

It is equivalent to if(!predicate(it)) it else null

takeUnless function example

A better way to measure Execution Time

We always check for execution time of our code in Java using the following pattern.

Execution Time logic in Java

We can write the same code using Kotlin syntax; Always remember with Kotlin there is always gonna be a better way to do something. It surprises me every time.

measureTimeMillis function example
It’s so beautiful

Just write your logic inside measureTimeMillis and that’s it! The same can is measureTimeNano for time in nano seconds.

All the functions mentioned above are a part of the Kotlin standard library which makes it more interesting.

Final thoughts

Kotlin is just full of these amazing eager functions. We just need to keep on exploring to find them. Leave a comment if you found some interesting easter functions. I’d be happy to hear.

Lastly it takes time and trial — error to understand which functions is suitable for your use-case.

Refer this Cheatsheat during confusion on selecting which function to use: https://lmller.github.io/kotlin-standard-extensions



Akshay Chordiya

Google Developer Expert @ Android | Android Engineer @ Clue | Instructor @Caster.IO