-
Notifications
You must be signed in to change notification settings - Fork 2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Kotlin @Binds error with covariant type parameter #1143
Comments
This seems relevant for #900. Probably has something to do with Kotlin's declaration site variance. The quick fix is probably to use |
I tried with Example: @Module
class ExampleModule {
@Provides
fun provideExample(example: ExampleImpl): Example<List<String>> = example
}
class Consumer @Inject constructor(private val example: @JvmSuppressWildcards Example<List<String>>) |
You might also need it on the bind. The bytecode window will show the signature of both places. |
I tried to add it also on the bind with no success. Consumer ExampleImpl Module The only way I avoided the error class ExampleImpl : Example<List<@JvmSuppressWildcards String>> But I can't inject it anymore. After this last change, I obtain: Consumer ExampleImpl Module The types seems to conform after the last change, but it doesn't work yet. |
Ok. I'd like to try it out and see what the bytecode says, hopefully tonight. In theory nothing should prevent you from making it work, we just have to figure out what is being generated and how to normalize it all to the right type declarations. |
I updated my comment with further information. |
My last try was the right one, simply I forgot the Short summaryInterface interface Example<T> Implementation class ExampleImpl @Inject constructor() : Example<List<@JvmSuppressWildcards String>> Module @Module
interface ExampleModule {
@Binds
fun provideExample(example: ExampleImpl): Example<List<String>>
} Consumer class Consumer @Inject constructor(private val example: @JvmSuppressWildcards Example<List<String>>) |
Closing this since it looks like #1143 (comment) has the solution. |
Given the interface:
The implementation:
The module:
The error
@Binds methods must have only one parameter whose type is assignable to the return type
is thrown at compile-time.Important notes
@Binds
and not with@Provides
So, this works:
T
is used as a covariant type parameter in the implementation ofExample
.So, this works:
The text was updated successfully, but these errors were encountered: