The combined use of several Kotlin features mixed with the use of generics allow to create functions that will greatly simplify your code, while maintaining its readability.

There are several functions included in the Kotlin library that are really useful, and once you have mastered some concepts they will be also very easy to use.

with function

Though there are several similar functions in the Kotlin standard library, I’m going to focus on with to break it into pieces.

What does this function allow? With it, we can make blocks of code that use a variable as its context, so that we don’t need to repeat its name every time we use it.

They can replace the builders, without creating a specific one for each class.

For example, going back to the case of the ViewGroup we had in the previous article, we could convert this code:

into this:


As you can see, the code inside the brackets behaves as if it were code inside the class itself.

How do we get this? We have already seen it before: with an extension function.

Extension functions as arguments for other functions

Things are getting more complicated, but this is so useful that you need to know about it.

You can define an extension function as a parameter for another function.

How would you implement the with function to be able to execute the previous example? The simplest would be this:

The previous code receives a ViewGroup as a parameter, and an extension function that applies to ViewGroup. Nothing prevents the ViewGroup from executing that function.

But this is too constrained. Are we going to need a similar function for each type of data?

Of course not.

Generic types

We can convert the above function to a generic one quite easily. Just replace ViewGroup with T:

Now it can be used by any type. An example:

But here we are missing out on an important power we were talking about at the beginning: that of acting as a builder.

Return a value of the Generic type

If we want it to act as a real builder, we need the constructed value to be returned somehow:

That way, our code would look like this:

sp() is a function from Anko library, which we have talked about before in this series of articles.

If you look at the official definition of the function, it is very similar to what we have done:

The main difference is that the extension function returns a value that may be different from the one being passed as an argument.

To achieve the same result using the regular with function, we would need to do the following:

The last line implies that the object that is executing the extension function will be returned.

Other interesting functions

There is a function that works very similar to what we wanted to get in the previous section, and that is called apply.


Instead of passing the object by parameter, this function acts as an extension function for that object:


If the corresponding object is not null, it will execute the code inside the function:

The text will be displayed in a toast only if both the TextView and the text are not null.


Using the power of generic types combined with extension functions, we can do very interesting things.

I encourage you to create your own functions that allow you to make your daily work easier.

Author: Antonio Leiva

I’m in love with Kotlin. I’ve been learning about it for a couple of years, applying it to Android and digesting all this knowledge so that you can learn it with no effort.