Search code examples
kotlindependency-injectiondagger-2dagger

Dagger2 & Kotlin: @Binds doesn't work with @IntoMap


I'm doing lesson from here: https://dagger.dev/tutorial/07-two-for-the-price-of-one

When I'm changing code

@Module
abstract class HelloWorldModule {
    @Binds
    abstract fun helloWorldCommand(command: HelloWorldCommand): Command
}

into

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

I'm getting error:

error: [Dagger/MissingBinding] Map<String,? extends Command> 
cannot be provided without an @Provides-annotated method.

What I'm missing here? It won't work on Kotlin?


Solution

  • Thanks @David Medenjak, you were right! Above code is ok, the problem was with missing @JvmSuppressWildcards, so my class CommandRouter now looks like:

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