Ben Madore
09/20/2019, 2:45 PMfun getFoo(): Foo? {
// lookup logic for foo
return foo?. someName { nullFunc = { <http://log.info|log.info>{"didn't find it"} }, nonNullFunc = { <http://log.info|log.info>{"Found $this"}} }
}
Adam Powell
09/20/2019, 2:50 PMfun getFoo(): Foo? = foo.also { theFoo ->
<http://log.info|log.info>(theFoo?.let { "Found $it" } ?: "didn't find it")
}
Ben Madore
09/20/2019, 2:58 PMalso
with ?:
can accomplish that.also
is only defined on a non-null receiverreturn foo
?.also { <http://log.info|log.info> { "Retrieved $it" } }
?: null.also { <http://log.info|log.info> { "Foo not found" } }
the null.also
was the main bit i really didn’t like, though, it appears to actually WORKViktor Penelski
09/20/2019, 4:24 PMif
can also be used within the initial .also
fun getFoo(): Foo? = lookupFoo().also {
if (foo != null) <http://log.info|log.info>("found $it!")
else <http://log.info|log.info>("didn't find it")
}