# Type mismatch, CapturedType expected

**URL:** <https://discuss.kotlinlang.org/t/type-mismatch-capturedtype-expected/20205>\
**Category:** Support\
**Created:** [December 10, 2020, 1:59pm UTC](https://discuss.kotlinlang.org/t/type-mismatch-capturedtype-expected/20205 "2020-12-10T13:59:56Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![VayKekaz](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/vaykekaz/32/7913_2.png) [@VayKekaz](https://discuss.kotlinlang.org/u/VayKekaz)\
**Post date:** [December 10, 2020, 1:59pm UTC](https://discuss.kotlinlang.org/t/type-mismatch-capturedtype-expected/20205/1 "2020-12-10T13:59:56Z")

</div>

I have this java code:

```java
class ScratchBot extends TelegramLongPollingBot {
    private final Action handler = new UpdateHandler();

    @Override public void onUpdateReceived(Update update) {
        BotApiMethod<out BotApiObject> action = 
                handler.createExecutableForChat(update.getMessage().getChatId());
        execute(action);
    }
}

interface Action {
    BotApiMethod<? extends BotApiObject> createExecutableForChat(long chatId);
}

class UpdateHandler implements Action {

    @Override
    public BotApiMethod<? extends BotApiObject> createExecutableForChat(long chatId) {
        return new SendMessage();
    }
}

```

It works perfectly in java, but when i convert it in Kolin it breaks and this code below doesn’t work.

```kotlin
internal class ScratchBot : TelegramLongPollingBot() {

    private val handler: Action = UpdateHandler()
    override fun onUpdateReceived(update: Update) {
        val action: BotApiMethod<out BotApiObject?> =
               handler.createExecutableForChat(update.message.chatId)
        execute(action)
    }
}

internal interface Action {
    fun createExecutableForChat(chatId: Long): BotApiMethod<out BotApiObject?>?
}

internal class UpdateHandler : Action {
    override fun createExecutableForChat(chatId: Long): BotApiMethod<out BotApiObject?> {
        return SendMessage()
    }
}

```

Compiler throws this exception on execute call

```auto
Type mismatch.
Required:
BotApiMethod<CapturedType(out BotApiObject?)!>!
Found:
BotApiMethod<out BotApiObject?>?

```

I use library [rubenlagus/TelegramBots: Java library to create bots using Telegram Bots API](https://github.com/rubenlagus/TelegramBots)  
And here is some its source code to show class hierarchy:

```java
// Signature of execute() method that's used in my java code, be careful, Method here is a TypeVariable
public <T extends Serializable, Method extends BotApiMethod<T>> T execute(Method method) { /* */ }
// What my handler actually returns
public class SendMessage extends BotApiMethod<Message> { /* */ }
// My handler return type is java's BotApiMethod<? extends BotApiObject> or kotlin's BotApiMethod<out BotApiObject>
public abstract class BotApiMethod<T extends Serializable> extends PartialBotApiMethod<T> { /* */ }
public class Message implements BotApiObject { /* */ }
public interface BotApiObject extends Serializable { /* */ }

```

I also should mention that first i tried to write it in kotlin, and only after struggling for ~2 days i rewrote the ScratchBot class in java. Yeah, it also works with kotlin classes, so even if i leave Action and UpdateHandler in kotlin, i still can use them in java without any problems.

---

<div class="post-metadata">

**Author:** ![nickallendev](https://avatars.discourse-cdn.com/v4/letter/n/cdc98d/32.png) [@nickallendev](https://discuss.kotlinlang.org/u/nickallendev)\
**Post date:** [December 10, 2020, 9:07pm UTC](https://discuss.kotlinlang.org/t/type-mismatch-capturedtype-expected/20205/2 "2020-12-10T21:07:20Z")

</div>

The issue is right there in the error message. I’ll try to highlight the difference.

Type mismatch.  
Required:  
BotApiMethod\<CapturedType(out BotApiObject?)!\> **!** ← not nullable BotApiMethod  
Found:  
BotApiMethod\<out BotApiObject?\> **?** ← nullable BotApiMethod

Either `createExecutableForChat` should not be returning a nullable type (is null a valid return value?), or `val action` should be a nullable type.

---

<div class="post-metadata">

**Author:** ![VayKekaz](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/vaykekaz/32/7913_2.png) [@VayKekaz](https://discuss.kotlinlang.org/u/VayKekaz)\
**Post date:** [December 11, 2020, 7:06am UTC](https://discuss.kotlinlang.org/t/type-mismatch-capturedtype-expected/20205/3 "2020-12-11T07:06:49Z")

</div>

Yeah, maybe that’s an issue, but the error message tells something different. I’ve just changed type to not-nullable, and nothing changed. I mentioned that first time i wrote this in kotlin and in case when i write in kotlin i don’t use nullable types. So there’s something different from just nullable / not-nullable type mismatch.

---

<div class="post-metadata">

**Author:** ![nickallendev](https://avatars.discourse-cdn.com/v4/letter/n/cdc98d/32.png) [@nickallendev](https://discuss.kotlinlang.org/u/nickallendev)\
**Post date:** [December 11, 2020, 8:21am UTC](https://discuss.kotlinlang.org/t/type-mismatch-capturedtype-expected/20205/4 "2020-12-11T08:21:31Z")

</div>

As I said the error message you posted is about passing a providing a nullable type where a no null type is expected.

Here’s an example of code that does compile (added stubs for the library)

```run-kotlin
class ScratchBot : TelegramLongPollingBot() {
    private val handler: Action = UpdateHandler()
    override fun onUpdateReceived(update: Update) {
        val action: BotApiMethod<out BotApiObject?> =
               handler.createExecutableForChat(update.message.chatId)
        execute(action)
    }
}
 interface Action {
    fun createExecutableForChat(chatId: Long): BotApiMethod<out BotApiObject?>
}
 class UpdateHandler : Action {
    override fun createExecutableForChat(chatId: Long): BotApiMethod<out BotApiObject?> {
        return SendMessage()
    }
}

open class BotApiMethod<T>
interface BotApiObject
abstract class TelegramLongPollingBot {
    open fun onUpdateReceived(update: Update) {} 
}
class Update(val message: Message) 
class Message(val chatId: Long) : BotApiObject

fun <T> execute(a: BotApiMethod<out T>) {} 
class SendMessage : BotApiMethod<Message>() 

fun main() {
    println("Hello world") 
}    

```

---

<div class="post-metadata">

**Author:** ![VayKekaz](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/vaykekaz/32/7913_2.png) [@VayKekaz](https://discuss.kotlinlang.org/u/VayKekaz)\
**Post date:** [December 11, 2020, 8:53am UTC](https://discuss.kotlinlang.org/t/type-mismatch-capturedtype-expected/20205/5 "2020-12-11T08:53:33Z")

</div>

Your `execute` method signature differs from the original.

```kotlin
// yours
fun <T> execute(a: BotApiMethod<out T>)
// original
fun <T : Serializable?, Method : BotApiMethod<T>?> execute(method: Method): T? = null

```

After i corrected it i’ve noticed that error message also changed to it:

```auto
Type mismatch.
Required:
Serializable?
Found:
Scratch_2.BotApiObject

```

So i also noticed that your signature of BotApiObject is also incorrect

```kotlin
// yours
interface BotApiObject
// original
interface BotApiObject : Serializable

```

And after correcting all your mistakes we’ve returned to starting point

```auto
Type mismatch.
Required:
Scratch_2.BotApiMethod<CapturedType(out BotApiObject)>?
Found:
Scratch_2.BotApiMethod<out Scratch_2.BotApiObject>

```

also i still don’t understand why is original `execute` method was made in such a weird way, it could be just

```kotlin
fun execute(method: BotApiMethod<out Serializable>): Serializable? = null

```

```java
Serializable execute(BotApiMethod<? extends Serializable> method) { return null; }

```

---

<div class="post-metadata">

**Author:** ![nickallendev](https://avatars.discourse-cdn.com/v4/letter/n/cdc98d/32.png) [@nickallendev](https://discuss.kotlinlang.org/u/nickallendev)\
**Post date:** [December 11, 2020, 8:39pm UTC](https://discuss.kotlinlang.org/t/type-mismatch-capturedtype-expected/20205/6 "2020-12-11T20:39:43Z")

</div>

> [@VayKekaz](#):
>
> Your `execute` method signature differs from the original.

It did not appear relevant based on the bug message (a simple nullable non-nullable mismatch) so I didn’t bother converting manually (was on phone).

> [@VayKekaz](#):
>
> And after correcting all your mistakes we’ve returned to starting point

It’d really be simpler to help if you included runnable code to start with. Start with ` ```run-kotlin` line and then end with a ````` line and the code between will be runnable like in my reply. If that’s not possible, including which line is actually showing the error is helpful. When I fixed those two errors, I did not get the same error. I saw an error related to passing `action` with covariant type `BotApiMethod<out BotApiObject>` into `execute` which expected an invariant type `BotApiMethod<T>`.

> [@VayKekaz](#):
>
> also i still don’t understand why is original `execute` method was made in such a weird way, it could be just

Not quite. `public <T extends Serializable, Method extends BotApiMethod<T>> T execute(Method method)` is not exactly the same as `public <T extends Serializable, Method extends BotApiMethod<? extends T>> T execute(Method method)` (see difference for the type argument of BotApiMethod).

The second which provides BotApiMethod with a covariant argument type (as opposed to the invariant definition of the first), when converted to Kotlin will give you:

```auto
fun <T : Serializable?, Method : BotApiMethod<out T>?> execute(method: Method): T?

```

Here’s some code that executes that hopefully doesn’t include any other errors:

```run-kotlin
import java.io.Serializable

fun <T : Serializable, Method : BotApiMethod<out T>> execute(method: Method): T { /* */
    return null as T //just so it will compile
}

internal class ScratchBot : TelegramLongPollingBot() {
    private val handler: Action = UpdateHandler()
    override fun onUpdateReceived(update: Update) {
        val action = handler.createExecutableForChat(update.message.chatId)
        execute(action)
    }
}

internal interface Action {
    fun createExecutableForChat(chatId: Long): BotApiMethod<out BotApiObject>
}

internal class UpdateHandler : Action {
    override fun createExecutableForChat(chatId: Long): BotApiMethod<out BotApiObject> {
        return SendMessage()
    }
}

// What my handler actually returns
class SendMessage : BotApiMethod<Message>() { /* */ }

// My handler return type is java's BotApiMethod<? extends BotApiObject> or kotlin's BotApiMethod<out BotApiObject>
abstract class BotApiMethod<T : Serializable> : PartialBotApiMethod<T>() { /* */ }
abstract class PartialBotApiMethod<T : Serializable>
class Message : BotApiObject { /* */ }
interface BotApiObject : Serializable { /* */ }
internal abstract class TelegramLongPollingBot {
    abstract fun onUpdateReceived(update: Update)
}

interface Update {
    val message: UpdateMessage
}

interface UpdateMessage {
    val chatId: Long
}
fun main() = println("Hello world")

```

---

<div class="post-metadata">

**Author:** ![VayKekaz](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/vaykekaz/32/7913_2.png) [@VayKekaz](https://discuss.kotlinlang.org/u/VayKekaz)\
**Post date:** [December 12, 2020, 5:23am UTC](https://discuss.kotlinlang.org/t/type-mismatch-capturedtype-expected/20205/7 "2020-12-12T05:23:43Z")

</div>

LMAOOOO kotlin is so bad!  
Look at this:

```java
public abstract class TelegramLongPollingBot {
    abstract void onUpdate(Update update);

    public <T extends Serializable, Method extends BotApiMethod<T>>
    T execute(Method method) {
        return null;
    }
}

```

```kotlin
abstract class TelegramLongPollingBotKt {
    abstract fun onUpdate(update: Update?)

    fun <T : Serializable, Method : BotApiMethod<T>>
    execute(method: Method): T {
        return null as T
    }
}

```

That’s two absolutely identical classes. But look here:

```kotlin
class ScratchBotWithJava : TelegramLongPollingBot() { // Java class

    private val handler: Action = UpdateHandler()
    override fun onUpdate(update: Update?) {
        val action = handler.createExecutableForChat(update!!.message.chatId)
        execute(action) // <-- Type mismatch
        //Required: BotApiMethod<CapturedType(out BotApiObject)!>!
        //Found: BotApiMethod<out BotApiObject>!
    }
}

class ScratchBotWithKotlin : TelegramLongPollingBotKt() { // Kotlin class

    private val handler: Action = UpdateHandler()
    override fun onUpdate(update: Update?) {
        val action = handler.createExecutableForChat(update!!.message.chatId)
        execute(action)
    }
}

private interface Action {
    fun createExecutableForChat(chatId: Long): BotApiMethod<out BotApiObject>
}

private class UpdateHandler : Action {
    override fun createExecutableForChat(chatId: Long): BotApiMethod<out BotApiObject> {
        return SendMessage()
    }
}

```

But if i correct java class’ `execute` method signature like this:

```java
public <T extends Serializable, Method extends BotApiMethod<? extends T>>
// now it has <? extends T> instead of just <T> right here ^
T execute(Method method) { return null; }

```

It compiles!  
So at this point i thought that kotlin compiles it’s

```kotlin
fun <T : Serializable, Method : BotApiMethod<T>> execute(method: Method): T

```

into java’s

```java
public <T extends Serializable, Method extends BotApiMethod<? extends T>> T execute(Method method);

```

But using javap tool from jdk i inspected both classes’ bytecode and signature was identical:

```auto
Compiled from "TelegramLongPollingBot.kt"
public abstract class telegram.bot.test.TelegramLongPollingBotKt {
  public final <T extends java.io.Serializable, Method extends org.telegram.telegrambots.meta.api.methods.BotApiMethod<T>> T execute(Method);
// More output, not neccessary

Compiled from "TelegramLongPollingBot.java"
public abstract class telegram.bot.test.TelegramLongPollingBot {
  public <T extends java.io.Serializable, Method extends org.telegram.telegrambots.meta.api.methods.BotApiMethod<T>> T execute(Method);
// more output

```

And look: signature only differs on `final` keyword, the rest of it is identical.

And the funniest part.  
The solution.

```kotlin
class WorkingBot : org.telegram.telegrambots.bots.TelegramLongPollingBot() { // not a mock

    // Here i override method just changing nullable generic types to not-nullable
    // that's signature that IntlliJ offers me:
    override fun <T : Serializable?, Method : BotApiMethod<T>?> execute(method: Method): T =
        super.execute(method)
    // and here's working signature, the solution
    override fun <T : Serializable, Method : BotApiMethod<T>> execute(method: Method): T =
        super.execute(method)

    private val handler: Action = UpdateHandler()
    
    override fun onUpdateReceived(update: Update?) {
        val action = handler.createExecutableForChat(update!!.message.chatId)
        execute(action)
    }
}

```

[VayKekaz/kotlin-java-interop-telegrambot (github.com)](https://github.com/VayKekaz/kotlin-java-interop-telegrambot) - the test repo with all the code.

---

<div class="post-metadata">

**Author:** ![andi-ttr](https://avatars.discourse-cdn.com/v4/letter/a/5f8ce5/32.png) [@andi-ttr](https://discuss.kotlinlang.org/u/andi-ttr)\
**Post date:** [February 17, 2022, 8:28am UTC](https://discuss.kotlinlang.org/t/type-mismatch-capturedtype-expected/20205/8 "2022-02-17T08:28:14Z")

</div>

I had exactly the same issue… thank you for sharing your solution!
