Ernestas Blog

Ernestas Blog

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.

Collections

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 {
    println(it)
}

numbers.contains(1)

numbers.filterNotNull()
    .map { it + 1 }
    .filter { it < 3 }
    .forEach { println(it) }

Lambdas

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.

numbers.map { number ->
    number * 10
}

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.

@JvmStatic
fun String.asPhpClass(project: Project): PhpClass? {
    return PhpIndex.getInstance(project).getClassesByFQN(this).firstOrNull()
}

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

"\App\Models\User".asPhpClass(project)

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 =
    this.textContains('*')

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)"
}

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 😅

 
Share this