Skip to content

Assisted inject of generic typed inputs isn't supported #454

@TheKeeperOfPie

Description

@TheKeeperOfPie

Basically, the following code doesn't work. I'm trying to create a ViewModel with a type parameter using assisted inject, but it fails with

[ksp] .../MyApplication/app/src/main/java/com/example/myapplication/Component.kt:17: Mismatched @Assisted parameters.
Expected: [savedStateHandle: androidx.lifecycle.SavedStateHandle, provider: TypedProvider]
But got:  [androidx.lifecycle.SavedStateHandle, com.example.myapplication.TypedProvider<kotlin.String>]
@Component
abstract class Component {
    abstract val viewModel: (SavedStateHandle, TypedProvider<String>) -> TypedViewModel<String>
}

@Inject
object Singleton

@Inject
class TypedViewModel<Type>(
    singleton: Singleton,
    @Assisted savedStateHandle: SavedStateHandle,
    @Assisted provider: TypedProvider<Type>,
)

interface TypedProvider<Type> {
    fun generate(): Type
}

There is a workaround by doing an inner factory class instead, but this feels like something that should be supported.

abstract class Component {
    abstract val viewModel: (SavedStateHandle) -> TypedViewModel.Factory
}

class TypedViewModel(...) {
    @Inject
    class Factory(
        private val singleton: Singleton,
        @Assisted private val savedStateHandle: SavedStateHandle
    ) {
        fun <Type> provide(provider: TypedProvider<Type>) =
            TypedViewModel(singleton, savedStateHandle, provider)
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions