Is there a way to have early-return behavior in composables from sub-functions without using exceptions?
In standard Kotlin, we can use exceptions for early return:
// this is not idiomatic, just an extremely simplified example
fun foo(bar: String): Int {
val int = bar.toIntOrNull()
requireNotNull(int) { "Early return here" }
return int
}
Using Arrow, the
either
comprehension enables similar features:
fun foo(bar: String): Either<String, Int> {
val int = bar.toIntOrNull()
ensureNotNull(int) { "Early return here" }
return int
}
Using Flows, similar behavior can be emulated using
catch
:
fun foo(bar: Stirng): Flow<Result> = flow {
val int = bar.toIntOrNull()
requireNotNull(int) { "Early return here" }
emit(Result.success(int))
}.catch {
if (it !is IllegalArgumentException) throw it
emit(Result.failure(it))
}
Which brings it to my question: what is the equivalent with
@Composable
functions?