Ben Madore
10/11/2019, 8:31 PMfun <T> T?.whenNull(block: T?.() -> Unit): T? {
if (this == null) {
block()
}
return this
}
to be used like:
return getFoo().whenNull {
<http://log.info|log.info> {"can't find a foo"}
}
jw
10/11/2019, 8:31 PM?: run { .. }
Ben Madore
10/11/2019, 8:35 PM<http://log.info|log.info>
returns Unit, so i’d need to return a null from the run, ya?return getFoo() ?: run {
<http://log.info|log.info> {"can't find a foo"}
null
}
which isn’t particularly expressive imojw
10/11/2019, 8:44 PMBen Madore
10/11/2019, 9:11 PMilya.gorbunov
10/11/2019, 10:39 PMalso
function suits such side checks well:
return getFoo().also {
if (it == null) <http://log.info|log.info>("Can't find foo")
}