Assign a function to another function

In Kotlin’s Coroutine channels doc in Prime numbers with pipeline section there is an example that is ambiguous.

fun CoroutineScope.numbersFrom(start: Int) = produce<Int> {
    var x = start
    while (true) send(x++) // infinite stream of integers from start
}

How produce function can assign to numbersFrom extension function ?
I didn’t see any example like this in the doc.
produce function returns ReceiveChannel interface . The code inside curly braces is the implementation of produce function last parameter which is a function type but where is the implementation of numbersFrom body and how this code works ?

this is the link of doc example:
https://kotlinlang.org/docs/reference/coroutines/channels.html#prime-numbers-with-pipeline

The code that you have shown is syntactic sugar. It is a function with so-called “expression body”. It has nothing to do with coroutines. Any function can use that expression body syntax. Your code is equivalent to:

fun CoroutineScope.numbersFrom(start: Int): ReceiveChannel<Int> {
    return produce<Int> {
        var x = start
        while (true) send(x++) // infinite stream of integers from start
    }
}
1 Like