Choosing a data structure to hold Coroutine Channels

Aha! Thank you so much.

You could roll your own delegate implementation to achieve this:

object Channels : DeclaredConstantList<BroadcastChannel<Message>>() {
   val myChannel by element { BroadcastChannel<Message>(1) }
   val myOtherChannel by element { BroadcastChannel<Message>(1) }
}

val allSubscriptions = Channels.map { it.openSubscription() }

which is backed by this implementation:

open class DeclaredConstantList<T>(
    private val all: MutableList<T> = mutableListOf()
) : List<T> by all {

    internal inline fun <V : T> element(element: () -> V): ReadOnlyProperty<Any, V> {
        val e = element()
        all += e
        return ConstantProperty(e)
    }
}

class ConstantProperty<out T>(val value: T) : ReadOnlyProperty<Any, T> {
    override fun getValue(thisRef: Any, property: KProperty<*>): T = value
}

Might be overkill, but I found it a nice way to explore building custom delegates :slight_smile: