How to sort based on/compare multiple values in Kotlin?

Say I have a class Foo(val a: String, val b: Int, val c: Date) and I want to sort a list of Foos based on all three properties. How would I go about this? Solution Kotlin’s stdlib offers a number of useful helper methods for this. First, you can define a comparator using the compareBy() method and pass it to […]

Read More

What is Kotlin?

What is Kotlin? Kotlin is a statically typed programming language that runs on the JVM (or Java Virtual Machine). It can be operated on multiple platforms and is compatible with Java, Android, JavaScript, as well as native applications. The language is developed by the team at JetBrains. JetBrains is committed to the project, has a […]

Read More

How to use Data Binding and Kotlin in Android Studio 3.0.0

I just started to use Android Studio 3.0.0, but every time I try to build my project I get this error: Error:Circular dependency between the following tasks: :app:compileDebugKotlin +— :app:dataBindingExportBuildInfoDebug | — :app:compileDebugKotlin (*) — :app:kaptDebugKotlin — :app:dataBindingExportBuildInfoDebug (*) (*) – details omitted (listed previously) I am using kapt “com.android.databinding:compiler:2.2.0” Before I was using androidProcessor […]

Read More

What is the equivalent of Java static methods in Kotlin?

There is no static keyword in Kotlin. What is the best way to represent a static Java method in Kotlin? Solution You place the function in the “companion object”. So the java code like this: class Foo { public static int a() { return 1; } } will become class Foo { companion object { […]

Read More

Why do we use “companion object” as a kind of replacement for Java static fields in Kotlin?

What is the intended meaning of “companion object”? So far I have been using it just to replace Java’s static when I need it. I am confused with: Why is it called “companion”? Does it mean that to create multiple static properties, I have to group it together inside companion object block? To instantly create […]

Read More

Kotlin – Property initialization using “by lazy” vs. “lateinit”

In Kotlin if you don’t want to initiate a class property inside the constructor or in the top of the class body, you have basically these two options ( from the language reference): Lazy Initialization lazy() is a function that takes a lambda and returns an instance of Lazy which can serve as a delegate […]

Read More

How to convert a kotlin source file to a java source file

I have a Kotlin source file, but I want to translate it to Java. How can I convert Kotlin to Java source? Solution In IntelliJ or Android Studio, you just have to do the following to get java code from kotlin: Menu>Tools>Kotlin>Show Kotlin Bytecode Click on the Decompile button Copy the java code This question was reposted […]

Read More

What Java 8 Stream.collect equivalents are available in the standard Kotlin library?

In Java 8, there is Stream.collect which allows aggregations on collections. In Kotlin, this does not exist in the same way, other than maybe as a collection of extension functions in the stdlib. But it isn’t clear what the equivalences are for different use cases. For example, at the top of the JavaDoc for Collectors […]

Read More

In Kotlin, what is the idiomatic way to deal with nullable values, referencing or converting them

If I have a nullable type Xyz?, I want to reference it or convert it to a non-nullable type Xyz. What is the idiomatic way of doing so in Kotlin? For example, this code is in error: val something: Xyz? = createPossiblyNullXyz() something.foo() // Error: “Only safe (?.) or non-null asserted (!!.) calls are allowed […]

Read More

Idiomatic way of logging in Kotlin

Kotlin doesn’t have the same notion of static fields as used in Java. In Java, the generally accepted way of doing logging is: public class Foo { private static final Logger LOG = LoggerFactory.getLogger(Foo.class); } Question is what is the idiomatic way of performing logging in Kotlin? Solution In majority of mature Kotlin code, you […]

Read More