DEV Community

Ernestas Kvedaras
Ernestas Kvedaras

Posted on • Originally published at

What do I like about Kotlin?

This year I've got to learn Kotlin because I wanted to develop a PhpStorm plugin. Kotlin was developed by Jetbrains and is open-sourced. The nice thing about it is that it can be easily mixed with Java and other languages.

Before I started getting into developing I've watched this Kotlin for Java Developers course. It was pretty helpful even though I don't work with Java. Plugin development wasn't easy, but there were a few things I've liked about the language itself.


Since I primarily work with Laravel (PHP) I was really happy to see a familiar concept of collections. All those nice map, filter, distinct, etc. methods are there. And I used them a lot 🙂

val numbers = listOf(null, 1, 2, null)

numbers.filterNotNull().forEach {


    .map { it + 1 }
    .filter { it < 3 }
    .forEach { println(it) }
Enter fullscreen mode Exit fullscreen mode


As you can see above it has some interesting syntax for map and filter for example. Such lambdas will always return the last statement which was something new to me. However, it makes sense and allows for very clean syntax for shorter methods.

it points to the current element of iteration. It is possible to change it when needed. { number ->
    number * 10
Enter fullscreen mode Exit fullscreen mode

Everything is an object?

Well, yes and no. Everything can have methods that we can call, but primitives have a special treatment and don't behave like objects (regarding reference). This sometimes allows a very expressive syntax.

fun String.asPhpClass(project: Project): PhpClass? {
    return PhpIndex.getInstance(project).getClassesByFQN(this).firstOrNull()
Enter fullscreen mode Exit fullscreen mode

In code, you may have places where you have a class fully qualified name and you need to find the class, just do this:

val classFQN = "\App\Models\User"

val class = calssFQN.asPhpClass(project)

// or even

Enter fullscreen mode Exit fullscreen mode

Add methods to any objects

Because you can add methods to strings, integers, etc., you can also add them to library objects. This may sound daunting at first, but it is actually very useful. For example, while building Laravel Query plugin, I had to do a lot of checks of classes and methods. Here are a few examples:

fun PhpClass.isJoinOrRelation(): Boolean =
    this.fqn == LaravelClasses.JoinClause || this.fqn == LaravelClasses.Relation

fun PsiElement.selectsAllColumns(): Boolean =
Enter fullscreen mode Exit fullscreen mode

Overloadable operators

I haven't actually used this one myself but sounds very cool. In kotlin, you can overload operators and describe how 2 objects should be added, subtracted, etc. Here is one of their examples:

data class Point(val x: Int, val y: Int)

operator fun Point.unaryMinus() = Point(-x, -y)

val point = Point(10, 20)

fun main() {
   println(-point)  // prints "Point(x=-10, y=-20)"
Enter fullscreen mode Exit fullscreen mode

However, what was mind-blowing initially for me is that things like in, a[i], a(i) are also operators. So a lot of language features are simply operators and added functions.

This is a very short list, but you get the idea 🙂 There are some more little things to like, I invite you to try it and maybe build some cool plugins for PhpStorm 😅

Top comments (0)