How can I assign a default channel to another channel if it’s empty, while retaining its original content when it’s not empty, considering the default value is also a channel?

Here’s one way to solve the problem of if one channel is empty, use another one.

params.fill = false

workflow {
    ch_something = params.fill ? Channel.of(1..3) : Channel.empty()
    ch_backfill = Channel.of(4..6).toList()
    ch_something.collect().concat(ch_backfill).first().flatMap().view()
}

This uses the property that collect only emits a list if there’s something in the channel. Use concat to make sure the channel contents stay in order. and then use first() to take the first list from the concatenated channels. If ch_something is empty, there will be nothing, and the list from ch_backfill is taken.

2 Likes