Colton Idle
07/14/2021, 9:10 PMList
?
e.g.
@Provides
fun provideRetrofit(interceptors: List<Interceptor>): MyApiService {
return MyRetrofitServiceBuilder().build(interceptors, "http://...")
}
@Provides
fun provideInterceptorList(@ApplicationContext context: Context): List<Interceptor> {
....
getting an error though.
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.ephemient
07/14/2021, 9:12 PMList<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 Javaephemient
07/14/2021, 9:12 PMMutableList<T>
is invariant, so it translates to List<T>
ephemient
07/14/2021, 9:13 PMList<@JvmSuppressWildcards T>
explicitly for this purposeColton Idle
07/14/2021, 9:15 PMephemient
07/14/2021, 9:15 PMephemient
07/14/2021, 9:16 PM@JvmSuppressWildcards
as it better matches the intentionAmanjeet Singh
07/14/2021, 9:19 PM@IntoSet
dagger multibindings if I am not wrong and provide a set collection instead if order not mattersColton Idle
07/14/2021, 9:22 PM@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.FunkyMuse
07/14/2021, 9:47 PMColton Idle
07/14/2021, 10:01 PMephemient
07/14/2021, 10:46 PM@IntoSet
has the same issue with covariance at the consumermiqbaldc
07/15/2021, 2:58 AMArray
instead of List
@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),
)