serializable in desired format in kotlin
serializable in desired format in kotlin
serializable in desired format in kotlin
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
Every time I hear about the “pass-by-value” vs “pass-by-reference” Java debate I always think the same. The answer I give: “Java passes a copy (pass-by-value) of the reference (pass-by-reference)”. So everyone is happy. I would say Kotlin does the same as it is JVM based language. UPDATE OK, so it’s been a while since this … Read more
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
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
There is a lateinit improvement in Kotlin 1.2 that allows to check the initialization state of lateinit variable directly: lateinit var file: File if (this::file.isInitialized) { … } See the annoucement on JetBrains blog or the KEEP proposal. UPDATE: Kotlin 1.2 has been released. You can find lateinit enhancements here: Checking whether a lateinit var … Read more
Suspending functions are at the center of everything coroutines. A suspending function is simply a function that can be paused and resumed at a later time. They can execute a long running operation and wait for it to complete without blocking. The syntax of a suspending function is similar to that of a regular function … Read more
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
Use :: to signify a function reference, and then: fun foo(msg: String, bar: (input: String) -> Unit) { bar(msg) } // my function to pass into the other fun buz(input: String) { println(“another message: $input”) } // someone passing buz into foo fun something() { foo(“hi”, ::buz) } Since Kotlin 1.1 you can now use … Read more
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