# Simple problem with an API (from Vert.x), which uses coroutines

**URL:** <https://discuss.kotlinlang.org/t/simple-problem-with-an-api-from-vert-x-which-uses-coroutines/10530>\
**Category:** Support\
**Created:** [November 26, 2018, 6:11pm UTC](https://discuss.kotlinlang.org/t/simple-problem-with-an-api-from-vert-x-which-uses-coroutines/10530 "2018-11-26T18:11:57Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![JohannesLichtenberger](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/johanneslichtenberger/32/6093_2.png) [@JohannesLichtenberger](https://discuss.kotlinlang.org/u/JohannesLichtenberger)\
**Post date:** [November 26, 2018, 6:11pm UTC](https://discuss.kotlinlang.org/t/simple-problem-with-an-api-from-vert-x-which-uses-coroutines/10530/1 "2018-11-26T18:11:57Z")

</div>

I have the following function declaration:

```
suspend fun <T> Context.executeBlockingAwait(blockingCodeHandler : Handler<Future<T>>) : T? {

```

with

```
@FunctionalInterface
public interface Handler<E> {

    /**
     * Something has happened, so handle it.
     *
     * @param event the event to handle
     */
    void handle(E event);
}

```

I don’t get this compiled ☹

```
private suspend fun foo(ctx: Context) {
    ctx.executeBlockingAwait { fut ->
        fut.complete("")
    }
}

```

`Type mismatch. Required: Handler<Future<String>> Found: (???) → [ERROR :]`

Also not with:

```
context.executeBlockingAwait<String> { future: Future<String> ->
    future.complete("bar")
}

```

`Type mismatch. Required: Handler<Future<String>> Found: (Future<String>) → Unit`

Isn’t this exactly how a handler should look like? Hmmm, I sadly don’t get it.

---

<div class="post-metadata">

**Author:** ![JohannesLichtenberger](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/johanneslichtenberger/32/6093_2.png) [@JohannesLichtenberger](https://discuss.kotlinlang.org/u/JohannesLichtenberger)\
**Post date:** [November 26, 2018, 6:41pm UTC](https://discuss.kotlinlang.org/t/simple-problem-with-an-api-from-vert-x-which-uses-coroutines/10530/3 "2018-11-26T18:41:04Z")

</div>

So

```
private suspend fun foo(ctx: Context) {
    ctx.executeBlockingAwait { fut ->
        fut.complete("")
        ""
    }
}

```

I thought that’s the return value from executeBlockingAwait(…)?

I’m getting

`Type mismatch. Required: Handler<Future<???>> Found: (???) → String`

But I thought I’m passing in the higher function, which is the Handler, and the handler has no return value (void). I forgot to mention that it’s a Java functional interface, but it might be obvious.

---

<div class="post-metadata">

**Author:** ![JohannesLichtenberger](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/johanneslichtenberger/32/6093_2.png) [@JohannesLichtenberger](https://discuss.kotlinlang.org/u/JohannesLichtenberger)\
**Post date:** [November 26, 2018, 6:42pm UTC](https://discuss.kotlinlang.org/t/simple-problem-with-an-api-from-vert-x-which-uses-coroutines/10530/4 "2018-11-26T18:42:48Z")

</div>

That said, the API would be super strange 😉

---

<div class="post-metadata">

**Author:** ![andreartus](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/andreartus/32/5754_2.png) [@andreartus](https://discuss.kotlinlang.org/u/andreartus)\
**Post date:** [November 26, 2018, 6:45pm UTC](https://discuss.kotlinlang.org/t/simple-problem-with-an-api-from-vert-x-which-uses-coroutines/10530/5 "2018-11-26T18:45:21Z")

</div>

> [@JohannesLichtenberger](#):
>
> private suspend fun foo(ctx: Context) { ctx.executeBlockingAwait { fut -\> fut.complete(“”) } }

I may have been too hasty. Future may not be what I expected.

---

<div class="post-metadata">

**Author:** ![JohannesLichtenberger](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/johanneslichtenberger/32/6093_2.png) [@JohannesLichtenberger](https://discuss.kotlinlang.org/u/JohannesLichtenberger)\
**Post date:** [November 26, 2018, 6:53pm UTC](https://discuss.kotlinlang.org/t/simple-problem-with-an-api-from-vert-x-which-uses-coroutines/10530/6 "2018-11-26T18:53:08Z")

</div>

Ist’s a Vert.x Future but just like a Promise/Future.

---

<div class="post-metadata">

**Author:** ![andreartus](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/andreartus/32/5754_2.png) [@andreartus](https://discuss.kotlinlang.org/u/andreartus)\
**Post date:** [November 26, 2018, 7:33pm UTC](https://discuss.kotlinlang.org/t/simple-problem-with-an-api-from-vert-x-which-uses-coroutines/10530/7 "2018-11-26T19:33:18Z")

</div>

What happens if you try something along this line?

```auto
    private suspend fun foo(ctx: Context) {
        val s: String? = ctx.executeBlockingAwait(object : Handler<Future<String>> {
            override fun handle(event: Future<String>) {
                return event.complete("")
            }
        })
    }

```

---

<div class="post-metadata">

**Author:** ![JohannesLichtenberger](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/johanneslichtenberger/32/6093_2.png) [@JohannesLichtenberger](https://discuss.kotlinlang.org/u/JohannesLichtenberger)\
**Post date:** [November 26, 2018, 7:49pm UTC](https://discuss.kotlinlang.org/t/simple-problem-with-an-api-from-vert-x-which-uses-coroutines/10530/8 "2018-11-26T19:49:58Z")

</div>

> [@andreartus](#):
>
> private suspend fun foo(ctx: Context) { val s: String? = ctx.executeBlockingAwait(object : Handler\<Future\<String\>\> { override fun handle(event: Future\<String\>) { return event.complete(“”) } }) }

Strange thing, okay it works, but what is this?

```
val s: String? = ctx.executeBlockingAwait(Handler<Future<String>> { event -> event.complete("") })

```

IntelliJ says it should be converted to a lambda… is this `Handler<Future<String>>` something like a cast, because the type inference rules don’t work? Maybe because of Java?

---

<div class="post-metadata">

**Author:** ![andreartus](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/andreartus/32/5754_2.png) [@andreartus](https://discuss.kotlinlang.org/u/andreartus)\
**Post date:** [November 26, 2018, 8:01pm UTC](https://discuss.kotlinlang.org/t/simple-problem-with-an-api-from-vert-x-which-uses-coroutines/10530/9 "2018-11-26T20:01:03Z")

</div>

I don’t have Vert.X so I just mimicked the interfaces you gave and wrote it up in Android Studio. I don’t have the same code you do, so had to make a few guesses.

From what I’ve gleaned both `Handler`, and `Future` can take type parameters of `Any`, so you need to help it along by being more specific. I’ll see if I can get something official to reference.

---

<div class="post-metadata">

**Author:** ![JohannesLichtenberger](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/johanneslichtenberger/32/6093_2.png) [@JohannesLichtenberger](https://discuss.kotlinlang.org/u/JohannesLichtenberger)\
**Post date:** [November 26, 2018, 8:18pm UTC](https://discuss.kotlinlang.org/t/simple-problem-with-an-api-from-vert-x-which-uses-coroutines/10530/10 "2018-11-26T20:18:43Z")

</div>

They just somehow generated these suspending extension methods, so I’m not sure. It’s in their 3.6.0-SNAPSHOT version and I’ve already posted on their mailing list a few days ago. I guess their intent was that one can use it with a simple lambda.

BTW: As I’m rather new to Kotlin, I currently don’t know what this explicit `Handler<Future<String>> { ... }` means. Is it meant as a shortcut for anonymous classes with an explicit type for the lambda?

---

<div class="post-metadata">

**Author:** ![andreartus](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/andreartus/32/5754_2.png) [@andreartus](https://discuss.kotlinlang.org/u/andreartus)\
**Post date:** [November 26, 2018, 8:23pm UTC](https://discuss.kotlinlang.org/t/simple-problem-with-an-api-from-vert-x-which-uses-coroutines/10530/11 "2018-11-26T20:23:27Z")

</div>

[https://kotlinlang.org/docs/reference/java-interop.html#sam-conversions](https://kotlinlang.org/docs/reference/java-interop.html#sam-conversions)

---

<div class="post-metadata">

**Author:** ![JohannesLichtenberger](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/johanneslichtenberger/32/6093_2.png) [@JohannesLichtenberger](https://discuss.kotlinlang.org/u/JohannesLichtenberger)\
**Post date:** [November 26, 2018, 8:27pm UTC](https://discuss.kotlinlang.org/t/simple-problem-with-an-api-from-vert-x-which-uses-coroutines/10530/12 "2018-11-26T20:27:09Z")

</div>

Oh okay, they (the Vert.x guys) need to change this, I think It’s rather ugly… and thank you 🙂
