Skip to content
This repository was archived by the owner on Dec 9, 2023. It is now read-only.
This repository was archived by the owner on Dec 9, 2023. It is now read-only.

Problem injecting generics #46

@emaleavil

Description

@emaleavil

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions