This is the second part of ‘Kotlin by examples’, you can find the first one here: https://medium.com/@dbottillo/kotlin-by-examples-class-and-properties-d57bf1d8dfa0
In this post I’m gonna talk more about function definitions and lambdas.
Just as a reminder of the first blog post, a function in Kotlin it’s defined with the keyword fun and can have one or more parameters with default values and a return type:
One of the most important feature in Kotlin, it’s that you can pass anonymous function (lambdas) as a parameter of a function.
Let’s take the classic Android click listener interface/implementation and convert it to a lambda in Kotlin.
If we want to rewrite that Java code in Kotlin you will do something like this:
So it’s not very different from Java, we are creating an object of that interface and define the override method right after. But In Kotlin it’s possible to substitute a function that receives an interface with a lambda:
So the idea is that our OnClickListener interface is actually just a lambda function with View as a parameter and Unit as a return type. You can think about Unit as null/void (since that function doesn’t return anything).
When you define a lambda function, it’s mandatory to use the brackets because you need to define the parameters to the left of the arrow and the actually implementation code on the right. But if the parameters are not used then Kotlin allows you to omit them:
And even better, if the function that you are passing is the last one in the parameters of the original function (in this case setOnClickListener function) then you can move out the parentheses:
So much better! Even further, if the lambda function is actually the only parameter then you can omit completely the parentheses:
That’s it. The 6 lines of Java code are now just one line very easy to read (and maintain!).
It’s nice to rewrite Java code in Kotlin because it becomes much nicer to read but Kotlin offers you way way more handy function. For example you have the with function available which let you call a block with a given object and returns its result:
So let’s imagine to have a standard onBindViewHolder function with a Car object to fill the view with. Usually you would do something like:
That code is full of duplications, if you use the with function the same code would be:
So the idea behind the with function is that inside the block of the function you have access to the initial object and then you can omit it.
That’s nice but you can also create your own functions, for example if we want a similar function to with in which we pass a block and we execute the block only if it’s a debug build of Android, in Java would it be:
Instead in Kotlin we can create a function with a block as parameter and call the function only if that condition is true:
In the first part we define a debug inline function with as a lambda parameter with no input parameter that returns nothing (Unit), then inside the inline function we can actually check if the condition is matched before executing the lambda. So in the onCreate method you call that inline function directly with a block (because like the OnClickListener() function, it doesn’t have any other parameters) making the code really easy to read in my opinion.
Another good example comes from the Anko library (https://github.com/Kotlin/anko) which is a library that provides to you all sort of handy methods in order to make the code more clean and readable. For example it’s possible to run some code in background and update the UI in just few lines of code:
Last example is also from Anko, when you need to use a database usually you need to create an sqlite helper, open the database, do your logic, close the database and handle all the exceptions. With Anko is possible to do:
So the code seems quite magic! You create an helper and then just call the use function with a block as input in which you can query your database and return the result. If you look at the source code of Anko, the use function definition is:
I think it’s a very interesting function, so it defines a type T and expect a lambda to be called on a SQLiteDatabase instance that returns a type T, the entire use function returns T as well. The implementation will automatically open the database for you (because you are calling the use function on a SQLiteDatabase), try to run the lambda and then close the database on the finally catch, returning the result of the lambda provided. So cool!
And that’s it for this blog post, I hope you have got a glimpse of the power of Kotlin in order to improve the readability and quality of your code base.
In the next post I will talk about all the handy features that Kotlin provides for list and dictionaries.