Is there a KEEP or youtrack issue for adding the c...
# language-proposals
s
Is there a KEEP or youtrack issue for adding the capturing subjects feature that
when
has, to more(or all) language control flows?
Copy code
when (val response = executeRequest()) {
            ...
        }
https://kotlinlang.org/docs/control-flow.html#when-expression
d
Can you elaborate please?
s
Smart cast to non null wouldn't work in a situation like this
Copy code
private 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
Copy code
if ((val sv = state.value) != null) {
       sv.collection
    }
d
You already can do it for `when`s AFAIK there is no such request for `if`s, so you can report one
👍 2
h
At least with state flows: the value could change in the time, so it would be better to use a custom variable, which works with smart casts.
y
Or in this case you can use a simple
state.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
s
Yes thanks. I usually prefer to just manually capture the value and write the logic with if-else's. I find it more readable
2