I have some problems unerstanding how Kotlin works...
# getting-started
m
I have some problems unerstanding how Kotlin works with Consumers and Producers. Actually, I’m going to convert a Telegram Chatbot written in Java to Kotlin. The library to access the Telegram API is written in Java and uses Consumers. This is the method signature in the Telegram Java API:
Copy code
public static Reply of(Consumer<Update> action, Predicate<Update>... conditions)
This is the Java code, where I make use of the above API method:
Copy code
return Reply.of(update -> silent.send("Nice pic!", getChatId(update)), Flag.PHOTO);
If I let IntelliJ IDEA convert this code to Kotlin, this is the result:
Copy code
return Reply.of({ update -> silent.send("Nice pic!", getChatId(update)!!) }, Flag.PHOTO)
Now, I get the following error message:
Copy code
Error:(77, 25) Kotlin: Type mismatch: inferred type is (Update) -> Optional<Message!>! but Consumer<Update!>! was expected
This is where I’m stuck. I know that Kotlin treats consumers and producers differently. What must the code look like? And why?