Using kotlinx.coroutines in IntelliJ IDEA project

runBlocking and other high-level coroutine utilities are not in the Kotlin standard library, but instead are a part of the library kotlinx.coroutines. To use this library in your project you must download its binaries and add a dependency on them to the project. Usually declaring a library dependency is a line or couple of lines … Read more

Kotlin: How to convert list to map with list?

Code fun main(args: Array<String>) { data class Combine(val alpha: String, val num: Int) val list = arrayListOf(Combine(“a”, 1), Combine(“b”, 2), Combine(“c”, 3), Combine(“a”, 4)) val mapOfList = list.associateBy ( {it.alpha}, {it.num} ) println(mapOfList) val changed = list .groupBy ({ it.alpha }, {it.num}) println(changed) } Output {a=4, b=2, c=3} {a=[1, 4], b=[2], c=[3]} How it works … Read more

Kotlin List tail function

Kotlin doesn’t have a built-in List<T>.tail() function, so implementing your own extension function is the only way. Although your implementation is perfectly fine, it can be simplified a bit: fun <T> List<T>.tail() = drop(1) Or, instead of extension function, you can define an extension property: val <T> List<T>.tail: List<T> get() = drop(1) val <T> List<T>.head: … Read more

When to use collect and collectLatest operator to collect kotlin flow?

Collect will collect every value , and CollectLatest will stop current work to collect latest value, The crucial difference from collect is that when the original flow emits a new value then the action block for the previous value is cancelled. flow { emit(1) delay(50) emit(2) }.collect { value -> println(“Collecting $value”) delay(100) // Emulate … Read more

“Leaking ‘this’ in constructor” warning should apply to final classes as well as open ones?

tl;dr: https://youtrack.jetbrains.com/issue/KT-22044 is a good fit regarding this issue. I will cite what Intellij IDEAs inspection called “Leaking ‘this’ in constructor” says about this: This inspection reports dangerous operations inside constructors including: Accessing a non-final property in constructor Calling a non-final function in constructor Using this as a function argument in a constructor of a … Read more

How to launch a Kotlin coroutine in a `suspend fun` that uses the current parent Scope?

You should make the function go an extension function of CoroutineScope: fun main() = runBlocking { go() go() go() println(“End”) } fun CoroutineScope.go() = launch { println(“go!”) } Read this article to understand why it is not a good idea to start in a suspend functions other coroutines without creating a new coroutineScope{}. The convention … Read more

How to call a lambda callback with mockk

You can use answers: val otm: ObjectToMock = mockk() every { otm.methodToCall(any(), any())} answers { secondArg<(String) -> Unit>().invoke(“anything”) } otm.methodToCall(“bla”){ println(“invoked with $it”) //invoked with anything } Within the answers scope you can access firstArg, secondArg etc and get it in the expected type by providing it as a generic argument. Note that I explicitly … Read more

What does “with” mean in Kotlin?

with is used to access an object’s members and methods without having to refer to the object once per access. It is (mostly) for abbreviating your code. It is frequently used when constructing an object: // Verbose way, 204 characters: var thing = Thingummy() thing.component1 = something() thing.component2 = somethingElse() thing.component3 = constantValue thing.component4 = … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)