https://kotlinlang.org logo
#coroutines
Title
# coroutines
l

Luis Munoz

04/28/2020, 7:17 AM
I have the following code
Copy code
protected suspend fun stdout(message: String, emit: suspend (WfResult) -> Unit) {
    emit(WfResult.Stdout(message))
}

protected suspend fun stderr(message: String, emit: suspend (WfResult) -> Unit) {
    emit(WfResult.Stderr(message))
}

open fun main() : Flow<Any>  = flow {
   stdout("someString" , ::emit)
 }
How do I make my own flow so I can use it like this (or how do I extend flow to add more methods):
Copy code
open fun main() : Flow<Any>  = myFlow {

   stdout("someString")
   stderr("someString")
 }
a

araqnid

04/28/2020, 10:57 AM
Make a subinterface of FlowCollector that includes stdout and stderr methods, and then write myFlow to take an extension method on it that delegates to an underlying flow’s emit methods
m

molikuner

04/28/2020, 12:23 PM
You could also do sth. like this:
Copy code
suspend fun FlowCollector<WfResult>.stderr(message: String) {
    stderr(message, ::emit)
}

suspend fun FlowCollector<WfResult>.stdout(message: String) {
    stdout(message, ::emit)
}
l

Luis Munoz

04/28/2020, 4:16 PM
thank you, way easier than I thought
3 Views