What are some favoured mechanisms for working getting rid of lateinit var ?
Specifically how would you get rid of this
Copy code
class A {
private lateinit b : B
init {
scope.launch { b = something }
}
fun doWithB() {
b.do()
}
}
p
Pablichjenkov
10/21/2024, 3:11 AM
val b = StateFlowB
scope.launch { b.update { something } }
b.collect { it.do() }
f
Fergus Hewson
10/21/2024, 4:06 AM
What if something errors, b will wait for ever
h
hfhbd
10/21/2024, 6:20 AM
b: Deferred
🤔 1
☝️ 1
s
Sam
10/21/2024, 6:23 AM
You can emulate a suspending constructor using a companion object. Maybe that helps you?
Copy code
class A private constructor(private val b: B) {
companion object {
suspend operator fun invoke(): A {
return A(b = somethingSuspending())
}
}
}
👀 2
p
Pablichjenkov
10/21/2024, 12:25 PM
In case of errors you should design B as a Result class ResultB class, Result.Success(b: B)/Result.Error(error: YourErrorBaseClass).
Another design, you can enclose the deferred or stateflow.collect in a withTimeout {} block