-
-
Notifications
You must be signed in to change notification settings - Fork 67
Open
Labels
bugSomething isn't workingSomething isn't working
Description
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
Labels
bugSomething isn't workingSomething isn't working