CLOVIS
10/20/2022, 8:14 AM// 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?Zach Klippenstein (he/him) [MOD]
10/20/2022, 3:42 PMCLOVIS
10/20/2022, 3:53 PMCLOVIS
10/20/2022, 3:54 PMPablichjenkov
10/20/2022, 7:15 PMCLOVIS
10/20/2022, 7:17 PMPablichjenkov
10/20/2022, 7:17 PMCLOVIS
10/20/2022, 7:18 PMPablichjenkov
10/20/2022, 7:18 PMCLOVIS
10/20/2022, 7:20 PM@Composable
fun Foo(user: User) {
ensure(user is Admin) { ... }
// We only reach this point if user is an admin
Text("I'm an admin!")
}
inline fun ensure(condition: Boolean, lazyMessage: () -> String) {
contract {
returns() implies condition
}
if (!condition)
// What here to return from Foo?
}
CLOVIS
10/20/2022, 7:21 PMreturn@Foo
and that would do exactly what I'm searching forPablichjenkov
10/20/2022, 7:22 PMPablichjenkov
10/20/2022, 7:28 PMBox(
modifier = Modifier
.background(Color.Red)
.fillMaxWidth(0.5F)
.height(redBoxHeightDp)
.absoluteOffset(y = 100.dp)
.onGloballyPositioned { coordinates ->
boxHeight = (coordinates.size.height.toFloat())
}
){ // BoxScope
return@Box
Text(text = "Pablo")
}
I get this.
androidx.compose.runtime.ComposeRuntimeError: Compose Runtime internal error. Unexpected or incorrect use of the Compose internal runtime API (Start/end imbalance). Please report to Google or use <https://goo.gle/compose-feedback>
"Start/end imbalance" Got me thinking🤔CLOVIS
10/20/2022, 7:31 PMPablichjenkov
10/20/2022, 7:40 PMCLOVIS
10/20/2022, 7:42 PMcompositionGroup.end()
or whatever on all function ends (e.g. all returns), it sounds like it's forgetting to do it in your case? Though I really don't knowPablichjenkov
10/20/2022, 8:26 PM