Day159 — Kotlin tips

Jacky Tsang
1 min readJan 4, 2023
  1. convert callback to coroutine to avoid callback hell using suspendCoroutine

callback:

suspendCoroutine:

note that suspendCoroutine does not change the CoroutineContext. It simply converts callback to suspend function which is more manageable.

2. out

declaration-site variance: you can annotate the type parameter T of Source to make sure that it is only returned (produced) from members of Source<T>, and never consumed.

https://kotlinlang.org/docs/generics.html#declaration-site-variance

3. inline function

https://www.baeldung.com/kotlin/inline-functions#inline-functions

When using inline functions, there is no extra object allocation and no extra virtual method calls.

--

--