A look into kotlin’s standard extension functions — part 1

Have you tried kotlin? Most of us would’ve tried to convert the existing java files to kotlin to see how the language looks like. Pretty much similar to swift. Please just don’t stop there after converting the java to kotlin. There is a lot you should know about it. The following article will give you some idea about how to write the converted kotlin files to actually be in kotlin style.

When you convert your existing java files to kotlin using the in-built kotlin converter (Cmd + Shift + A or Ctrl + Shift + A and just type “convert” you should see “Convert Java file to kotlin file” in the list) in your Android studio, It would’ve converted all the lines into kotlin.

Unfortunately this won’t squeeze your code much. You’ll have to write manually to squeeze your code further. Assuming you’ve knowledge about Kotlin’s extension function, function expression, function extension expression and Higher order function. If you don’t have any idea about what I just said, I would recommend you to go watch Android Development with Kotlin — Jake Wharton first. Let’s squeeze our existing code

Let’s assume you want to compose a email, below is a typical example for how you’d achieve it in java

Uhhh Doesn’t this look ugly? I have the instance intent but still you’ve to mention it every time to assign a property or to call a function like putExtra(). Okie let me make it prettier in kotlin using the apply extension function from the Kotlin’s Standard.kt

All the functions inside this file Standard.kt are kotlin extension functions. This makes our life easier without writing the boilerplate code over and over

Now your code looks clean. You can access you’re receiver's (i.e Intent) properties/functions directly without repeating the instance over and over.

You can use it for any builder pattern like following. (UPDATE: But you’re basically doing the same thing with apply extension. It’s actually an overuse of apply here but It’s your choice to follow a style. There is no hidden cost)

Hold on. Wait… What happened here? Please stop showcasing code.

Okie. To understand it bit clear, Let’s take a look at the apply extension function explanation given in the kotlin’s Standard.kt file

Calls the specified function [block] with `this` value as its receiver and returns `this` value.
public inline fun <T> T.apply(block: T.() -> Unit): T { block(); return this }

So the instance (i.e intent) on which you’re using apply call will be returned back to you and the function expression { } uses this as its receiver to call its properties/functions.

Use case:

When you’ll have to call an instance over and over to assign properties or to call functions. For example, before making a network call using retrofit you might have to assign payload with more properties like the following example.

This will help you avoid writing the boilerplate for you.


Okie. Everything is cool now. How about nested apply? Let’s take a look at a typical java example of creating a notification in android first.

In the above example we are creating a simple android notification from scratch. Let’s convert this into kotlin using our apply

In the above code, line #1 and #6 has an label to reference the object. Because without these labels, You won’t be able to differentiate between which this object you want to access inside the method addnextIntent() in line #9. When you nest any type of extension function, Using labels are the best way to avoid confusion.

It’s easy to create a label for any extension function. Just put the label name before the @ symbol right before your expression brace starts. For more about labels click here

We could’ve written the resultIntent part outside the builder. Just to show you the power of kotlin with nested extension functions and lables I did it purposely. I don’t nest too much like this unless it is required. Because end of the day readablility of your code is very much important.

Thank you for reading