android / codelab-android-hilt

Apache License 2.0
348 stars 184 forks source link

Problem injecting generics #46

Open emaleavil opened 1 year ago

emaleavil commented 1 year ago

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