# Any reason the suspend keyword is not supported with anonymous functions?

**URL:** <https://discuss.kotlinlang.org/t/any-reason-the-suspend-keyword-is-not-supported-with-anonymous-functions/24478>\
**Category:** Language Design\
**Created:** [March 27, 2022, 3:28pm UTC](https://discuss.kotlinlang.org/t/any-reason-the-suspend-keyword-is-not-supported-with-anonymous-functions/24478 "2022-03-27T15:28:19Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![donaldtam](https://avatars.discourse-cdn.com/v4/letter/d/ad7895/32.png) [@donaldtam](https://discuss.kotlinlang.org/u/donaldtam)\
**Post date:** [March 27, 2022, 3:28pm UTC](https://discuss.kotlinlang.org/t/any-reason-the-suspend-keyword-is-not-supported-with-anonymous-functions/24478/1 "2022-03-27T15:28:19Z")

</div>

I like anonymous functions, they have different semantics than lambdas while still being easily assignable to a variable for callbacks etc.  
But any reason they do not work with the keyword suspend?

---

<div class="post-metadata">

**Author:** ![angelos-project](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/angelos-project/32/9468_2.png) [@angelos-project](https://discuss.kotlinlang.org/u/angelos-project)\
**Post date:** [March 27, 2022, 4:30pm UTC](https://discuss.kotlinlang.org/t/any-reason-the-suspend-keyword-is-not-supported-with-anonymous-functions/24478/2 "2022-03-27T16:30:39Z")

</div>

I can confirm that anonymous functions can’t be suspended. The following expression failed:

```auto
        @Suppress("UNCHECKED_CAST")
        example.runLambda((fun(): String = "world") as suspend () -> String)

```

Here is an example on how to declare use of suspendible lambda, implementation of suspendible lambda and use of suspendible lambda:

```auto
package example

import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.launch
import kotlin.coroutines.EmptyCoroutineContext

interface Example {
    // Declare method that takes a suspendable lambda lambda
    fun runLambda(hello: suspend () -> String)
}

class ExampleImpl : Example {
    private val scope = CoroutineScope(EmptyCoroutineContext)

    // Implement the method
    override fun runLambda(hello: suspend () -> String) {
        scope.launch { println("Hello, ${hello()}!") }
    }
}

fun main(args: Array<String>) {
    val example = ExampleImpl()
    // Then use the lambda
    example.runLambda {
        "world"
    }
}

```

---

<div class="post-metadata">

**Author:** ![donaldtam](https://avatars.discourse-cdn.com/v4/letter/d/ad7895/32.png) [@donaldtam](https://discuss.kotlinlang.org/u/donaldtam)\
**Post date:** [March 28, 2022, 3:19am UTC](https://discuss.kotlinlang.org/t/any-reason-the-suspend-keyword-is-not-supported-with-anonymous-functions/24478/3 "2022-03-28T03:19:27Z")

</div>

absolutely amazing reply brother, I am just a bit confused by the design choice to allow ordinal functions and lambda functions to be suspended but not anonymous functions in particular.

---

<div class="post-metadata">

**Author:** ![angelos-project](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/angelos-project/32/9468_2.png) [@angelos-project](https://discuss.kotlinlang.org/u/angelos-project)\
**Post date:** [March 28, 2022, 11:43am UTC](https://discuss.kotlinlang.org/t/any-reason-the-suspend-keyword-is-not-supported-with-anonymous-functions/24478/4 "2022-03-28T11:43:41Z")

</div>

Hard to know why
