Exploring Easter functions in Kotlin
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!
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
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.
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.
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.
apply function
apply
is an extension function for all types of object. Functionality — wise it is pretty much similar to with
function.
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.
You can even give custom name if you want.
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:
Kotlin style:
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.
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
A better way to measure Execution Time
We always check for execution time of our code in Java using the following pattern.
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.
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