为什么匕首图可在Java中工作,但在Kotlin中却说我提供后缺少Provides? [英] Why the dagger graph works works in java but in Kotlin it says missing Provides when I have provided?

查看:89
本文介绍了为什么匕首图可在Java中工作,但在Kotlin中却说我提供后缺少Provides?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

将匕首接口从Java转换为Kotlin时遇到一些问题

I have some issues when converting dagger interfaces from java to Kotlin

如果没有@Provides注释的方法,就无法提供[Dagger/MissingBinding] java.util.Map.

I got [Dagger/MissingBinding] java.util.Map cannot be provided without an @Provides-annotated method.

这是我的界面

   interface TopicConfigModule {
    @Binds
    @IntoMap
    @StringKey(NAME)
    fun bindCommandHandler(handler: TopicCommandHandler): CommandHandler

    companion object {
        @JvmStatic
        @Provides
        @FragmentScope
        fun provideHubsConfig(
            commandRegistry: Map<String, CommandHandler>
        ): Config {
            return ...
        }
    }
}

而CommandHandler是Java接口

and CommandHandler is java interface

public interface HubsCommandHandler {```}


推荐答案

Map在Kotlin中是协变的(方差)的值类型(public interface Map<K, out V>),而Java中的Map不是.您的函数将被翻译为

Map in Kotlin is covariant (variance) on its value type (public interface Map<K, out V>), but Map in Java is not. Your function will be translated to

Config provideHubsConfig(Map<String, ? extends CommandHandler> commandRegistry) { ... }

,但匕首恰好提供了Map<String, CommandHandler>.因此,我们需要使用@JvmSuppressWildcards

but dagger provides exactly Map<String, CommandHandler>. So we need to suppress wildcards with @JvmSuppressWildcards

commandRegistry: Map<String, @JvmSuppressWildcards CommandHandler>

从Java调用Kotlin-泛型

这篇关于为什么匕首图可在Java中工作,但在Kotlin中却说我提供后缺少Provides?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆