Kotlin static methods and variables

The closest thing to Java’s static fields is a companion object. You can find the documentation reference for them here: https://kotlinlang.org/docs/reference/object-declarations.html#companion-objects Your code in Kotlin would look something like this: class Foo { companion object { lateinit var instance: Foo } init { instance = this } } If you want your fields/methods to be … Read more

How to run Kotlin class from the command line?

Knowing the Name of Your Main Class To run a Kotlin class you are actually running a special class that is created at the file level that hold your main() and other functions that are top-level (outside of a class or interface). So if your code is: // file App.kt package com.my.stuff fun main(args: Array<String>) … Read more

What does ?: do in Kotlin? (Elvis Operator)

TL;DR: If the resulting object reference [first operand] is not null, it is returned. Otherwise the value of the second operand (which may be null) is returned. Additionally, the operator can throw an exception if null is returned. The Elvis operator is part of many programming languages, e.g. Kotlin but also Groovy or C#. I … Read more

Kotlin’s Iterable and Sequence look exactly same. Why are two types required?

The key difference lies in the semantics and the implementation of the stdlib extension functions for Iterable<T> and Sequence<T>. For Sequence<T>, the extension functions perform lazily where possible, similarly to Java Streams intermediate operations. For example, Sequence<T>.map { … } returns another Sequence<R> and does not actually process the items until a terminal operation like … Read more

Jooq fetchInto with default value if field is null

The reason is that JSON_ARRAYAGG() (like most aggregate functions) produces NULL for empty sets, instead of a more “reasonable” empty []. Clearly, you never want this behaviour. So, you could use COALESCE, instead, see also this question: coalesce( jsonArrayAgg(jsonObject(book.ID, book.PRICE)), jsonArray() ) I’ll make sure to update all the other answers I’ve given on Stack … Read more