# Problem with inline functions with coroutines and exposed

**URL:** <https://discuss.kotlinlang.org/t/problem-with-inline-functions-with-coroutines-and-exposed/3255>\
**Category:** Libraries\
**Created:** [June 12, 2017, 10:14am UTC](https://discuss.kotlinlang.org/t/problem-with-inline-functions-with-coroutines-and-exposed/3255 "2017-06-12T10:14:52Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![klaasjanelzinga](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/klaasjanelzinga/32/2763_2.png) [@klaasjanelzinga](https://discuss.kotlinlang.org/u/klaasjanelzinga)\
**Post date:** [June 12, 2017, 10:14am UTC](https://discuss.kotlinlang.org/t/problem-with-inline-functions-with-coroutines-and-exposed/3255/1 "2017-06-12T10:14:52Z")

</div>

Hi,

The following code does not compile. It is based on the [pipeline example](https://github.com/Kotlin/kotlinx.coroutines/blob/master/kotlinx-coroutines-core/src/test/kotlin/guide/example-channel-04.kt). The problem is in the produceNumbers function. The send call of the producer is embedded in a lambda. This works if the lambda is inline. (see `someInlineLamda`). The `transaction` call is not inline and gives a compile error (`Error:(24, 13) Kotlin: Suspension functions can be called only within coroutine body`).

If I move the transaction code to another inline lambda (see `someInlineTransactionLambda`) the lambda needs to be crossinline due to non-local returns but the error remains.

Any ideas on how to resolve this?

```
import kotlinx.coroutines.experimental.*
import kotlinx.coroutines.experimental.channels.*
import org.jetbrains.exposed.sql.transactions.transaction

inline fun <E> someInlineLambda(x: Int, function: (Int) -> E): Unit {
    for (i in 0..1) 
        function(x)
}

inline fun <E> someInlineTransactionLambda(x: Int, crossinline function: (Int) -> E): Unit {
    transaction {
        for (i in 0..1) {
            function(x)
        }
    }
}

fun produceNumbers() = produce<Int>(CommonPool) {
    var x = 1
    while (true) {
        someInlineLambda(x) {
            send(x++) // OK - inline lambda
        }
        transaction { // start transaction in exposed land
            send(x++) // Error:(24, 13) Kotlin: Suspension functions can be called only within coroutine body
        }
        someInlineTransactionLamda { // start transaction in exposed land, wrapped in inline lambda
            send(x++) // did not work :(
        }
    }
}

fun square(numbers: ReceiveChannel<Int>) = produce<Int>(CommonPool) {
    for (x in numbers) send(x * x)
}

fun main(args: Array<String>) = runBlocking<Unit> {
    val numbers = produceNumbers() // produces integers from 1 and on
    val squares = square(numbers) // squares integers
    for (i in 1..5) println(squares.receive()) // print first five
    println("Done!") // we are done
    squares.cancel() // need to cancel these coroutines in a larger app
    numbers.cancel()
}

```

---

<div class="post-metadata">

**Author:** ![elizarov](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/elizarov/32/2164_2.png) [@elizarov](https://discuss.kotlinlang.org/u/elizarov)\
**Post date:** [June 13, 2017, 9:46am UTC](https://discuss.kotlinlang.org/t/problem-with-inline-functions-with-coroutines-and-exposed/3255/2 "2017-06-13T09:46:56Z")

</div>

To make non-inline function work with suspensions you’ll have to explicitly mark the functions and their lambda arguments with `suspend` keyword.

---

<div class="post-metadata">

**Author:** ![klaasjanelzinga](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/klaasjanelzinga/32/2763_2.png) [@klaasjanelzinga](https://discuss.kotlinlang.org/u/klaasjanelzinga)\
**Post date:** [June 13, 2017, 5:16pm UTC](https://discuss.kotlinlang.org/t/problem-with-inline-functions-with-coroutines-and-exposed/3255/3 "2017-06-13T17:16:54Z")

</div>

Ok got it. Thanks.
