Learn Kotlin with me — Understanding Open Source — Part 0

Hitesh Kohli
2 min readMar 22, 2024

--

Photo by Wes Hicks on Unsplash

More and more developers use Kotlin as their main language but struggle with its foundations. I am no different, I struggled with the basics of Kotlin in my interviews. So I decided to change that attitude. I decided to become better in Kotlin, to start from the basics and go to advance. Also along the way understand some open source as well.

It will be an interesting journey.

In this series, we are going from variables to advanced Algorithms and experimenting with open source. I hope I learn a lot and so do you. Let’s get started.

To start this series, let us open an open-source project and try to find an issue:

So I opened up an open-source project and found these extension functions. They have placed these functions to solve linting errors in the project. Since these functions are empty they might cause unexpected behavior. So let’s try to solve this issue.

private fun <E> List<E>.add() {

}

As you can see this is a simple extension function of List.add(). Let’s try to manipulate it a little.

private fun <E> MutableList<E>.add() {

}

Firstly, we will make it mutable to solve the immutability problem.

private fun <E> MutableList<E>.add() {
this.add()
}

Let’s introduce a simple add inside it to add the elements to the list.

private fun <E> MutableList<E>.add(value:E) {
this.add(value)
}

Now let’s introduce a value through the constructor of the add function and pass it on to add.

It was a simple fix.

Now let’s try to use it.

val List : MutableList<String> = mutableListOf<String>("Hitesh", "Rahul")
List.add("sam")list.forEach{
println(it)
}

It will simply print all the values from the list

Hitesh
Rahul
sam

If we go deep inside the workings of the add function, we will see that it points to the add extension function we created. It means that the add function is working properly.

So, we have just solved an open-source issue. I hope you enjoyed this simple example.

Thank you for reading. If I have written something wrong please forgive me. Thank you for your most precious thing, your attention and your time.

Here is the video for this article:

--

--

Hitesh Kohli
Hitesh Kohli

Written by Hitesh Kohli

Hi, my name is Hitesh Kohli, I work at Geeks for Geeks as an Android developer. I love messing around with apps and games.

No responses yet