I want to write a null check code in a fancy way . Is there a better way to write? Some sample or tips will be great! I would love to hear from you!
if (list != null) {
myData = list[0]
}
A more functional style of writing this is using let
together with the ?
operator:
list?.let { myData = it[0] }
Using ?
, let
is only called if 'list' is not null
and list
is passed as the only parameter to the lambda.