Home > Mobile >  Is close() and channel.close() the same for channelFlow (or callbackFlow)?
Is close() and channel.close() the same for channelFlow (or callbackFlow)?

Time:10-12

I have a callbackFlow below

fun <T> Flow<T>.callbackMerge(other: Flow<T>): Flow<T> = callbackFlow {
    channel.close()
    launch {
        collect { trySend(it) }
        channel.close()
    }
    other.collect { trySend(it) }
    awaitClose { println("Close Callback") }
}

I can use channel.close() or close(). They seem to behave the same to me.

Is there a different between them? When should I use which?

CodePudding user response:

The docs of channel property say:

A reference to the channel this coroutine sends elements to. It is provided for convenience, so that the code in the coroutine can refer to the channel as channel as opposed to this. All the SendChannel functions on this interface delegate to the channel instance returned by this property.

So basically they are the same due to delegation.

  • Related