Kotlin @Binds error with covariant type parameter
See original GitHub issueGiven the interface:
interface Example<T>
The implementation:
class ExampleImpl: Example<List<String>>
The module:
@Module
interface ExampleModule {
@Binds
fun provideExample(example: ExampleImpl): Example<List<String>>
}
The error @Binds methods must have only one parameter whose type is assignable to the return type
is thrown at compile-time.
Important notes
- it happens only with
@Binds
and not with@Provides
So, this works:
@Module
class ExampleModule {
@Provides
fun provideExample(example: ExampleImpl): Example<List<String>> = example
}
- it happens only when
T
is used as a covariant type parameter in the implementation ofExample
. So, this works:
class ExampleImpl: Example<MutableList<String>>
@Module
interface ExampleModule {
@Binds
fun provideExample(example: ExampleImpl): Example<MutableList<String>>
}
Issue Analytics
- State:
- Created 5 years ago
- Reactions:2
- Comments:8
Top Results From Across the Web
Force type parameter to be invariant at use-site when it is ...
Coding example for the question Force type parameter to be invariant at use-site when it is covariant at declaration site-kotlin.
Read more >Kotlin Generics: Trouble migrating from Java - Stack Overflow
You can solve it adding a new type parameter which other languages ... and the other type parameter as covariant types (using out...
Read more >Generics: in, out, where | Kotlin
The general rule is this: when a type parameter T of a class C is declared out , it may occur only in...
Read more >Dagger2 covariant Set multibinding - Google Groups
As context: I found this while prototyping Dagger2 with Kotlin, ... error: @Binds methods must return a primitive, an array, a type variable,...
Read more >Dependency Injection using the Typeclassless technique
The function multiplyBy2 , as defined above, receives FT: Functor<F> as a parameter. We need to bind FT to this to be able...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
My last try was the right one, simply I forgot the
@Inject
annotation on the constructor ofExampleImpl
. The following summary is for people who encounter the same problem. Thanks for your help, considering that now it works, I close the issue, feel free to re-open it if needed.Short summary
Interface
Implementation
Module
Consumer
I tried with
@JvmSuppressWildcards
but it doesn’t help in this case. It’s still needed using the@Provides
annotation.Example: