Dagger2 和 Kotlin:@Binds 不适用于 @IntoMap

tho*_*n86 1 dependency-injection kotlin dagger dagger-2

我正在从这里上课:https ://dagger.dev/tutorial/07-two-for-the-price-of-one

当我更改代码时

@Module
abstract class HelloWorldModule {
    @Binds
    abstract fun helloWorldCommand(command: HelloWorldCommand): Command
}
Run Code Online (Sandbox Code Playgroud)

进入

@Module
abstract class HelloWorldModule {
    @Binds
    @IntoMap
    @StringKey("hello")
    abstract fun helloWorldCommand(command: HelloWorldCommand): Command
}
Run Code Online (Sandbox Code Playgroud)

我收到错误:

error: [Dagger/MissingBinding] Map<String,? extends Command> 
cannot be provided without an @Provides-annotated method.
Run Code Online (Sandbox Code Playgroud)

我在这里缺少什么?它在 Kotlin 上不起作用吗?

tho*_*n86 5

谢谢@David Medenjak,你是对的!上面的代码没问题,问题是缺少@JvmSuppressWildcards,所以我的类CommandRouter现在看起来像:

@JvmSuppressWildcards
class CommandRouter @Inject constructor(
    val outputter: Outputter,
    val commands: Map<String, Command>
) {
// ...
}
Run Code Online (Sandbox Code Playgroud)