Two indispensable Kotlin functions, you don’t want to miss.

Johannes Braun
3 min readJun 1, 2017

--

Since the Google I/O 2017 Kotlin is now officially supported by Google, so it’s about time for all Android developers, to dig into it.
There are now plenty of “getting into Kotlin” articles out there, so in this one I just want to concentrate on two neat functions from the Kotlin standard library let() and apply() which potentially will make your code much more concise.

Let(s) write no more if statements

Let() is the first function I’am going to explain. So lets have a look at the official documentation:

official documentation from https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/let.html

Looks complicated? Actually it’s pretty simple: let is defined as inline function and operates on any object of type T, which is passed into a block R, that you can define. T is then available in your block as anonymous parameter. Have a look at this example:

We’ve an address class inside our user. If we want to do something with it, we can call let() on it and get itself passed into the lambda block. Via it we have access to it. In line 8 we created an scoped variable, which is only visible within its block. We can do something, like concat the city and zipCode property from our address object into it and pass it to another function outside of our let() block.

Although this example code may not make much sense, you may understand how to use let() to group code and make it more concise. However the real power of let comes into play when combined with Kotlins safe call operator :

In the second example we just changed the address object inside our user to be nullable in line 1. Now if we want to do something with our address object we would normally check it inside an if statement to be not null. With let() and the safe call operator we can just do it like in line 6: The lambda block from let() will only be called, if address is not null, hence address inside our block is guaranteed to be not null, so we can have safe access to all its properties. In the last line we’re even able to realise an else statement. It means that if address was really null, so the let() block was not called, we just display a Toast notification. Pretty cool, isn’t it? Just keep in mind not to overuse this pattern. Several nested let() calls for example are badly readable.

Taking it further with apply()

The second function you will probably use very often, is apply()

official documentation from https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/apply.html

It looks pretty similar to let(), with one big difference, it returns the input type at the end. It’s very useful when initialising new objects or grouping setter calls together in one logical block. Notice in the next example how apply() returns the created new Bundle object, so we can assign it to our variable:

You can also do the same trick as with let() in combination with the safe call operator. For example we want to remove the first child view from a nullable parent component, which would look like this:

For this example we could of course also have used the let() function. This is more or less up to your taste or if you want to do something directly with the returned value, then you should of course use let().

That’s already it. Happy and productive coding!

--

--