What are some favoured mechanisms for working gett...
# random
f
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
val b = StateFlowB scope.launch { b.update { something } } b.collect { it.do() }
f
What if something errors, b will wait for ever
h
b: Deferred
🤔 1
☝️ 1
s
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
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