Kotlin is a developer-friendly language, and it makes the most challenging job(development) in the world a fun job, no matter in which platform you’re working. With Kotlin, you can create your style of code.

If you haven’t read the first part, I highly recommend you read it. The first article contains a detailed guide on how you use powerful & useful features from Kotlin like extensions, higher-order, and scoped functions.

Having said that, what are you going to learn in this article?
Let’s say we can’t fit the hidden yet powerful features of Kotlin in one or two articles.

Let’s start learning

Kotlin Delegates

Let’s see a simple example

Simple delegate example

If you’ve any doubts with syntax, check out in Kotlin Docs here.

What happens here is that we can assign a value to the variable str_sample through as shown above, but the advantage of writing this is that you don’t need to write .trim() every time.

You can create extensions or custom classes to apply this even more quickly. Read part1 to know how to create extensions?

Isn’t that cool, you might wonder something like it’s sweet but not super useful.

You’re mistaken, it’s super useful, let’s see how

Do you guys remember how we write shared-preference put and get functions, let me jog your memory with a sample

preference without delegates

Can we reduce the code and make it look smart. Of course, we can,

preference with delegates

Now, let’s see how we can use these delegates with other components. If you observe what we are doing here carefully, you can see that we’re saving a value to a variable and retrieving the value later when required.

We can leverage this functionality and implement saving & retrieving values from the bundle in fragments way more easily.

Let’s see how we save & retrieve values from the bundle in fragments.

Bundle operations without delegates

What we did here is that we save the values into the bundle then assign that bundle to the fragment while creating and later retrieving the values from the bundle in OnCreate function.

You see, it’s too much work to do; lets make it a bit easy with extensions and delegates.

First, let’s create an extension to insert values in the bundle so that we don’t need to invoke different functions based on datatype. Have a look

Kotlin extension to insert values in Bundles

Then we need to create a class that extends ReadWriteProperty and override set and get functions as shown below to save and retrieve real-time values. Have a look

Custom Delegate class to automate bundle functionality in fragments

Now create a function to simplify the process of using above class and also easy to understand for new developers in your project.

fun <T : Any> argument(): ReadWriteProperty<Fragment, T> 
= FragmentArgumentDelegate()

Now its time to use this delegate and see how it makes our lives easy.

Handling bundle values with Kotlin delegates

That’s all you don’t even need to write any code to put or get values from bundle. Now isn’t that awesome.

Default & Named parameters in functions

Let’s say we’ve to write a function that calculates the sum of two values. The tricky part here is that the user can pass both values or any one of the values or nothing. I’m sure java can handle this as shown below.

Now, let’s see how we can handle this in Kotlin.

fun add(a : Int = 0, b : Int = 0) =  a + b

Isn’t this fantastic — a function with parameters having default values. You can pass only a or b value based on your requirements.

Now, how can you pass only a or b values when the function has two parameters. How will the function know which value you’re passing?

This is another useful feature in Kotlin; we can pass a parameters using their names in the actual function. Have a look

Inline functions to use higher-order functions effectively

An inline function is declared with a keyword inline. The use of inline function enhances the performance of the higher-order function. The inline function tells the compiler to copy parameters and functions to the call site.

Let’s say the following print is an inline function.

public inline fun print(message: String) {
System.out.print(message)
}

Now let’s invoke the print function from another function have a look.

fun normalFunction(){
print("Hello")
print("world")
}

This is how it appears before compilation, when we declare a function as inline compiler treats it differently. While compiling, it replaces the function call with actual statements in that function. Have a look at normalFunction body after compilation.

fun normalFunction(){
System.out.print("Hello")
System.out.print("world")
}

So that no objects are saved in the memory, and it executes the statements step by step as the normal function does, which leads to better performance.

The mighty when statement in Kotlin

But when has a plethora of useful functionalities like it can return the value or execute code blocks based on the values.

Simple ‘when’ block

when(number) {
0 -> println("apple")
1, 2 -> println("banana")
3 -> println("grape")
4 -> println("Orange")
else -> println("Not a fruite")
}

when replaces default keyword in switch to else but the functionality is similar. In my opinion else fit into the syntax than default.

Return values with ‘when’ block

when with return value

This kind of functionality is not possible in java.

Arbitrary conditions

when with arbitrary conditions

Using ‘when’ without argument