CLOVIS
12/14/2022, 3:37 PMcontext(Raise<T>) @Composable do? Does it even work?
Throwing exceptions inside of composables is not allowed (it bubbles up weirdly). so I'm not sure if this is allowed or not.simon.vergauwen
12/14/2022, 3:39 PM@Composable. It also doesn't behave normally with CoroutineScope, right? You need to use LaunchedEffect?CLOVIS
12/14/2022, 3:40 PM@Composable and suspend on a single function. But since post 2.0 we won't need suspend on shift …simon.vergauwen
12/14/2022, 3:41 PMBut since post 2.0 we won't needWe're still doing some investigation 😅onsuspend…shift
simon.vergauwen
12/14/2022, 3:42 PM@Composeable why it should remain suspend, or where removing suspend is problematic I'd love to hear itsimon.vergauwen
12/14/2022, 3:43 PMIf you find use-cases inOr elsewhere.why it should remain@Composeablesuspend
CLOVIS
12/14/2022, 3:43 PMsuspend , we currently can't use shift inside of them (though I don't even know if that would make sense), so removing this requirement potentially makes it possible to have error management directly in composables (but again, composable functions are weird so I don't know if it even makes sense)simon.vergauwen
12/14/2022, 3:44 PMCLOVIS
12/14/2022, 3:47 PM// Convenience function to use Either services with Compose
@Composable
fun <E, T> rememberEither(compute: suspend () -> Either<E, T>): Either<E, T> {
var state by remember { mutableStateOf(/* trust me, I have an initial value here */) }
LaunchedEffect {
state = compute()
}
return state
}
@Composable
fun Foo(id: String) {
val data = rememberEither { service.getById(id) }
.orNull()
if (data != null) {
Data(…)
} else {
LoadingSpinner()
}
}CLOVIS
12/14/2022, 3:49 PMif in the composable appears every time there is an API result (since they're all modeled as either-returning functions).
It's a bit ugly, and feels like what the either{} block is here to avoid, yet composables don't seem to like it 😅CLOVIS
12/14/2022, 3:52 PMsuspend represents "a computation that takes some time", EffectScope<T> represents "a computation that may fail and short-circuit", @Composable is "a computation that may have new results over time" (if my understanding is correct, it's isomorphic to a StateFlow, hence Molecule), but it doesn't seem like EffectScope and @Composable compose (eh) that well together.CLOVIS
12/14/2022, 3:54 PMCLOVIS
12/14/2022, 3:56 PM@Composable
fun <T, O> Either<T, O>.fold(
onLeft: @Composable (T) -> Unit,
onRight: @Composable (O) -> Unit,
)
I'll experiment with that.CLOVIS
12/14/2022, 3:57 PMfold is inline, right? So that may already be available.
At some point I should experiment more with that and write a Arrow + Compose blog post.CLOVIS
12/14/2022, 3:59 PMsimon.vergauwen
12/14/2022, 4:01 PMinline. This is a very interesting use-case for Either inside Compose! I would say using Either rather then Raise is safer, because then you're working with values between recomposition.Marko Novakovic
12/14/2022, 4:02 PMMarko Novakovic
12/14/2022, 4:02 PMCLOVIS
12/14/2022, 4:03 PM@Composable
fun LaunchedEffect(block: context(Raise<T>) suspend () -> Unit)CLOVIS
12/14/2022, 4:04 PMMarko Novakovic
12/14/2022, 4:04 PMMarko Novakovic
12/14/2022, 4:05 PMMarko Novakovic
12/14/2022, 4:06 PMCLOVIS
12/14/2022, 4:07 PMCLOVIS
12/14/2022, 4:07 PMMarko Novakovic
12/14/2022, 4:07 PMMarko Novakovic
12/14/2022, 4:08 PMCLOVIS
12/14/2022, 4:08 PMMarko Novakovic
12/14/2022, 4:09 PMsimon.vergauwen
12/14/2022, 4:09 PMMarko Novakovic
12/14/2022, 4:11 PMMarko Novakovic
12/14/2022, 4:11 PMsuspend Compose is doing that with @Composablesimon.vergauwen
12/14/2022, 4:11 PMCLOVIS
12/14/2022, 4:12 PMCLOVIS
12/14/2022, 4:12 PMMarko Novakovic
12/14/2022, 4:13 PMHow is reading from DB or writing to file not necessary? 😅I fucked up there, kind of. it’s not as necessary as UI, let’s say it like that. it’s important ofc but program would be usable without DB but without UI not that much
Marko Novakovic
12/14/2022, 4:13 PMMarko Novakovic
12/14/2022, 4:15 PMsuspendMarko Novakovic
12/14/2022, 4:15 PMCLOVIS
12/14/2022, 4:17 PMsuspend () -> Either<E, T> to having the value in a local variable in a @Composable function, that doesn't require additional nesting to manage the different cases. IMO it's really the only thing needed for Arrow and Compose to fit well together. I'll continue searching, but I'm sure there's a wayMarko Novakovic
12/14/2022, 4:18 PM@Composable
fun Composable() {
Text(text = "Hello World!")
}
would never throw, for example. only reason it would is if you are getting text from somewhere else and UI should not care about it. that’s my pointMarko Novakovic
12/14/2022, 4:19 PMCLOVIS
12/14/2022, 4:19 PMCLOVIS
12/14/2022, 4:20 PMCLOVIS
12/14/2022, 4:20 PMMarko Novakovic
12/14/2022, 4:22 PMraise, no throw etc.
• I want to read that blogpost, I may be wrong and that may be really cool approach. for sure sounds pretty coolCLOVIS
12/14/2022, 4:24 PMEither is basically just a generic version of that with nice syntax sugar, so I don't think it's an egregious request 🙂Javier
12/14/2022, 5:26 PMJavier
12/14/2022, 5:28 PM