codelab-android-hilt icon indicating copy to clipboard operation
codelab-android-hilt copied to clipboard

Problem injecting generics

Open emaleavil opened this issue 3 years ago • 0 comments

Hi,

I am facing problems with kotlin generics that use in or out to specify generics. The case that I was trying to inject is the following:

@Binds
 abstract fun bindAddEntryUseCase(
     useCase: AddEntryUseCase
 ): UseCase<AddEntry, Result<Unit>>
@HiltViewModel
class AddViewModel @Inject constructor(
    @AddUseCase private val useCase: UseCase<AddEntry, Result<Unit>>
) : ViewModel() { ... }
class AddEntryUseCase @Inject constructor() : UseCase<AddEntry, Result<Unit>> {
    override suspend fun execute(params: AddEntry): Result<Unit> {
        ...
        return Result.success(Unit)
    }
}

Result signature is class Result<out T> and when I try to inject Hilt throws the below error:


@Binds methods' parameter type must be assignable to the return type
    public abstract com.eeema.android.domain.usecase.UseCase<com.eeema.android.domain.model.AddEntry, kotlin.Result<kotlin.Unit>> bindAddEntryUseCase(@org.jetbrains.annotations.NotNull()

I don't know why is not possible to inject generics with in or out types. Version of Hilt: 2.43.2

emaleavil avatar Aug 16 '22 08:08 emaleavil