How to be a friend to Kotlin

Ann Krepchenko
AndroidPub
Published in
3 min readMar 6, 2017

Few days ago Kotlin 1.1 was released. It means that all Kotlin-haters who tell me about “dead language”, “white elephant”, etc. are crushed. Until they come up with new arguments, you can try Kotlin.

If you are a java-thinking developer — this story is for you.
Honestly, if you are not — it is for you too.
I don’t want to repeat after someone and tell you about basics. It is just a story about my fails (and how to avoid them), my delights (and how to catch them) and about some fun cases. So, lets start.

Imagine, you are creating a new project, may be pet-project, to test Kotlin. You have already visited kotlinlang.org, read some articles and listened at least one conference session. You decided it is time to try. Same do I. Excited with null-safety I used .? operator at all places I can. Fortunately, my common sense came back to me.

Making arrays

The first cool thing I noticed was method arrayOf() and its brothers. If you are lucky to use sqlite database with CursorLoaders in your application you just need:

instead of:

I was very surprised with kotlin plugin in an Android Studio. The marking errors with underwave works perfect for me. With val/var as params too. So, if you will remove .toString() from first example plugin notifies you about error immediatly.

Data classes

The second grand feature is data classes. Only one sentence about them — from over 50 lines it magically becomes only 1. Sounds perfect, doesn’t it? And now it is time for fails. My colleague asked how to use RealmObjects with Kotlin correctly. We found official example and seems problem was resolved, but.. But I was searching better solution for few hours more. Unfortunately, the best solution was in realm github. It looks like

And there no ways to make data classes and realm friends. =(

Static utils

I am a fan of utils used to simplify my life. To find my own solution for utils I got over 3 ways.

  1. companion object, guide way to have access from my classes
  2. object, as android studio suggest to convert
  3. object + companion object
    More details about last. I suggest use object for classes with static methods such as image picking util and different companion objects inside class for utils to manage database at all, for example, and tables of cats, dogs and parrots in cases.

Default values

It is block about fun and fails. You may be surprised, but during long time I wasn’t used this feature. Why? Just because I forgot about it! Really makes your code shorter with avoiding simiular anought methods. Now, you have only one toast(..), instead of realization with duration value and without.

Maps/lists

In my humble opinion, maps and lists are most popular collections in android app development. You cannot avoid the time when you have to use them. I was so light to be sure that this would work

“Haha”, — said me Android Studio and marked “put” with red color. What?? I checked the code few times more. “What is wrong, what is wrong, what is wrong” was in my head. I jumped into the Map, HashMap. It was suspicious. I went to read the documentation. And what was my surprise to see the Map supports only read-only access. Hello, the fail. The read-write access is supported through the MutableMap interface. Then I finally read the documentation about all collections in Kotlin. Meet the correct usage:

The same thing happened with your favorite lists. By the way, don’t forget to look through collections in Kotlin one more time, if you have already met. There are some additional tasty features in Kotlin 1.1.

Now I find that post is too long and I have more interesting information(about constructors and adapters, for example). So, see you in next part.

And just remember — Kotlin is already your good friend.

To be continued…

--

--