Should I be able to @Provide a `List` ? e.g. ```...
# dagger
c
Should I be able to @Provide a
List
? e.g.
Copy code
@Provides
fun provideRetrofit(interceptors: List<Interceptor>): MyApiService {
    return MyRetrofitServiceBuilder().build(interceptors, "http://...")
}

@Provides
fun provideInterceptorList(@ApplicationContext context: Context): List<Interceptor> {
....
getting an error though.
Copy code
error: [Dagger/MissingBinding] java.util.List<? extends okhttp3.Interceptor> cannot be provided without an @Provides-annotated method.
  public abstract static class SingletonC implements RollerToasterApplication_GeneratedInjector,
Using an ArrayList instead works, but just curious if I'm missing something basic here.
e
because immutable
List<out T>
is declared as covariant in the type parameter, Kotlin translates
List<Interceptor>
to
List<? extends Interceptor>
in Java, which is different than
List<Interceptor>
in Java
MutableList<T>
is invariant, so it translates to
List<T>
but there is an annotation,
List<@JvmSuppressWildcards T>
explicitly for this purpose
c
Oh shit. Wait. So what's the "proper" way forward? Use mutableList or the annotation route?
e
either way works, I'd go with
@JvmSuppressWildcards
as it better matches the intention
a
You can also use
@IntoSet
dagger multibindings if I am not wrong and provide a set collection instead if order not matters
c
Order matters in this case because I'm using interceptors, but overall I was just curious why I can't Provide a List. Makes sense though. For my final solution I just did this
Copy code
@Provides
fun provideRetrofit(interceptors: @JvmSuppressWildcards List<Interceptor>): MyApiService {
    return MyRetrofitServiceBuilder().build(interceptors, "http://...")
}
I thought I would need @JvmSuppressWildcards on the provider return type itself, but I did not.
f
Just provide those interceptors into set using multi bindings, it's more convenient than a list
c
I need a guaranteed order
e
BTW,
@IntoSet
has the same issue with covariance at the consumer
👍 1
m
The same issues we found, but we’re using an
Array
instead of
List
Copy code
@Singleton
    @Provides
    fun provideSubmissionService(
        authenticator: Authenticator,
        interceptors: Array<out Interceptor>,
    ): SubmissionService =
        makeRetrofit(
            baseUrl = BuildConfig.BASE_SUBMISSION_URL,
            authenticator = authenticator,
            interceptors = interceptors,
        ).create(
            SubmissionService::class.java
        )

    @Singleton
    @Provides
    fun provideInterceptors(
        @ApplicationContext context: Context,
        cache: AccessTokenCache,
    ): Array<Interceptor> = arrayOf(
        ChuckerInterceptor(context),
        BearerTokenInterceptor(cache),
    )