Another approach would be using when
with val
Example:
when(val a = b.value) {
null -> {}
else -> {} //Value of a can be accessed inside else block as not-null
}
A drawback with this approach is even if you don't want to run anything when value is null, you must specify null
case with empty block.