Transition Page Recycler using Animated Recycler View

Advanced Kotlin Tips

Advanced Kotlin Tips on writing good Kotlin code and using what the language has to offer. There are many benefits of using Kotlin; it is concise, safe and most importantly it is 100% interoperable with Java. It also tries to solve some of the limitations of Java.
Top 12 Advanced Kotlin Tips For Pro Developers
Now is a good time to consider Kotlin for your next big feature or project, because Google finally announced it to be a first-class language for writing Android apps in Google I/O 17.

So here are some advanced Kotlin tips to help you start making use of what the language has to offer and writing good Kotlin code.

1. Singleton

Implementing lazy loaded and thread-safe singletons in Kotlin is really easy, unlike Java where you would have to rely on the complex double-checked locking pattern. And although Java enum singletons are thread-safe, they are not lazy loaded.
  1. object Singleton {
  2. var s: String? = null
  3. }
Contrary to a Kotlinclass, an object can’t have any constructor, but initblocks can be used if some initialization code is required.
  1. Singleton.s = “test” // class is initialized at this point
The object will be instantiated and its init blocks will be executed lazily upon first access, in a thread-safe way.

Comments