iex
03/29/2020, 7:12 PMval foo: Foo = optionalFoo ?: run {
log.e("Not set, can't continue!")
return
}
grahamborland
03/29/2020, 7:43 PMoptionalFoo?.let { foo ->
// body of function
}
// end of function
Shawn
03/29/2020, 7:48 PMShawn
03/29/2020, 7:57 PMoptional*
naming and just using if
to guard —
val foo: Foo? = getFooOptional()
if (foo == null)
log.error("foo not set, can't continue")
return
}
println(foo.bar) // can use foo here normally w/o fear of NPE thanks to smart casting
molikuner
03/29/2020, 8:56 PMval foo = nullableFoo ?: logger.e("message").also { return }
It works for me, as I think there shouldn't be much logic in such a case. In my opinion if that case needs more than one line of code it should be refactored anyways, to keep to code readable.Shawn
03/29/2020, 8:58 PMiex
04/03/2020, 9:42 AM