Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dagger 2 multibindings with Kotlin

I have the following snippet in my dagger 2 module

@Singleton @Provides @ElementsIntoSet fun providesQueries(foo: Foo): Set<Foo>{     val queries = LinkedHashSet<Foo>()     queries.add(foo)     return queries } 

I try to inject into in this way

@Inject lateinit var foo: Set<Foo> 

But dagger shows an error which says that Dagger cannot provides java.util.Set without @Provides or @Produces method.

I did the same in java and it worked. Does somebody know why is it failing?

like image 438
Borja Avatar asked Mar 31 '17 13:03

Borja


People also ask

Does dagger work with Kotlin?

Dagger is a fully static, compile-time dependency injection framework for Java, Kotlin, and Android.

What is dagger 2 Android Kotlin?

What is Dagger2? Dagger2 is a static compile-time dependency injection Framework for Java,Kotlin and Android. It should actually be Dagger, Dagger2 simply implies the second version which was a complete re-write of the DI framework. The earlier version was created by Square. Dagger2 is now maintained by Google.

What is Multibinding in dagger?

Intro to IntoSet. In a nutshell, Set multibinding allows you to inject a collection of a specific type into other classes, and also provides a way for you to inject a specific class into that collection.


1 Answers

As it described in the Kotlin reference

To make Kotlin APIs work in Java we generate Box<Super> as Box<? extends Super> for covariantly defined Box (or Foo<? super Bar> for contravariantly defined Foo) when it appears as a parameter.

You can use @JvmSuppressWildcards for avoiding it, just as following:

@Inject lateinit var foo: Set<@JvmSuppressWildcards Foo> 
like image 73
Aleksandr Avatar answered Sep 21 '22 05:09

Aleksandr