Mark
09/25/2020, 4:46 AM::emit
)?
suspend fun FlowCollector<Something>.emitNotNull(something: Something?) {
something?.let { emit(it) }
}
spand
09/25/2020, 4:49 AMMark
09/25/2020, 4:49 AMnanodeath
09/25/2020, 4:51 AM?.let(FlowCollector::emit)
not work?spand
09/25/2020, 4:51 AMMark
09/25/2020, 4:55 AMalso
but then get similar:
Type mismatch.
Required:
(TypeVariable(T)) → Unit
Found:
KSuspendFunction1<Something, Unit>
Mark
09/25/2020, 4:58 AMsuspend fun FlowCollector<Something>.emitNotNull(something: Something?) {
something?.suspendingAlso(::emit)
}
suspend inline fun <T> T.suspendingAlso(block: suspend (T) -> Unit): T {
block(this)
return this
}
spand
09/25/2020, 5:04 AM::emit
is this::emit
. I am not sure this is what you want? ie. creating a bound reference on every call. In any case I think this is much more readable (but maybe your example is contrived):
suspend fun FlowCollector<String>.emitNotNull(something: String?) {
if (something != null) emit(something)
}
Mark
09/25/2020, 5:05 AMspand
09/25/2020, 5:14 AMFlowCollector::emit
is actually a two parameter function. One for the receiver and one for the value to emit. This doesnt work nicely with let and method reference. The reason it works as a lambda is that receiver is passed implicitly.Mark
09/25/2020, 6:07 AM::emit
would work if emit was not suspending?
fun FlowCollector<Something>.notSuspending(something: Something?) {
}
suspend fun FlowCollector<Something>.emitNotNull(something: Something?) {
something?.also(::emit) // does not compile
something?.also(::notSuspending) // does compile
}
spand
09/25/2020, 6:13 AMthis::emit
as a work-around to my issue (and not generally desirable).Mark
09/25/2020, 6:39 AMthis::emit
synonymous with ::emit
?spand
09/25/2020, 6:47 AMFlowCollector::emit
spand
09/25/2020, 6:48 AM