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