Saiedmomen
04/15/2022, 7:28 AMwhen
has, to more(or all) language control flows?
when (val response = executeRequest()) {
...
}
https://kotlinlang.org/docs/control-flow.html#when-expressiondmitriy.novozhilov
04/15/2022, 7:49 AMSaiedmomen
04/15/2022, 8:20 AMprivate val _stateFlow = MutableStateFlow<State?>(null)
val state: StateFlow<State?> = _stateFlow
if (state.value != null) {
state.value.collection // open or custom getter
}
Is there a proposal to support capturing the value in control flow statements itself?
something, very crudely like
if ((val sv = state.value) != null) {
sv.collection
}
dmitriy.novozhilov
04/15/2022, 8:21 AMhfhbd
04/15/2022, 9:42 AMYoussef Shoaib [MOD]
04/16/2022, 3:26 PMstate.value?.let {}
. It even works if you need extra conditions state.value?.takeIf { it.followsACustomSetOfChecks() }?.let {}
and you can have an else using the elvis operator ?:
(keep in mind though that the elvis will trigger if let
returns a null, so a better idea is to use also
instead of let
Saiedmomen
04/17/2022, 9:07 AM