question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[AssistedInject] Support Generic Factory

See original GitHub issue

Right now we use a Factory and provider to inject generic factories:

I.e:

class ViewModelFactory<VM : ViewModel>
@Inject constructor(private val provider: Provider<VM>) : ViewModelProvider.Factory {
    @Suppress("UNCHECKED_CAST")
    override fun <T : ViewModel> create(modelClass: Class<T>): T = provider.get() as T
}
@Inject lateinit var factory: ViewModelFactory<MyViewModel>

This works great in many places, I was trying to do the same with the assisted factory to achieve something similar, but the AssistedFactory requires a 1-1 mapping from what I can tell and will return an error if there is no matching @AssistedInject

@AssistedFactory
interface StateViewModelFactory<VM : ViewModel> {
    fun create(handle: SavedStateHandle): VM
}
/// An assisted factory's abstract method must return a type with an @AssistedInject-annotated constructor.

Would love to be able to do something similar:

@Inject lateinit var factory: StateViewModelFactory<MyViewModel>

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:11
  • Comments:6

github_iconTop GitHub Comments

3reactions
chrisjenxcommented, Nov 17, 2021

@gusuly0rum the work around right now isn’t ideal but not terrible.

I have a generic abstract factory:

interface StateViewModelFactory<VM : ViewModel> {
    fun create(handle: SavedStateHandle): VM
}

Then I just create a concrete factory for each one. Would be good to simpliy this down, but for the most part it’s just a one liner still:

@AssistedFactory
interface Factory : StateViewModelFactory<GroceryCheckoutVM>
0reactions
chrisjenxcommented, Nov 19, 2022

The whole point of the ticket is the AssistedInject doesn’t work…

Read more comments on GitHub >

github_iconTop Results From Across the Web

Problem with generic return type in guice assisted inject factory
Does anybody know, why guice 3 doesn't like the generic parameter in the factory method or what I generally misunderstood about assisted inject...
Read more >
assisted inject and generic factory - Google Groups
So far i got the following code to be working: class Node<T>{} - a base class for nodes, lets say nodes in a...
Read more >
FactoryModuleBuilder (Google Guice 5.0.1 API)
Provides a factory that combines the caller's arguments with injector-supplied values to construct objects. Defining a factory.
Read more >
Assisted Injection - Dagger
A factory is typically responsible for combining all of the parameters and creating the object. (Related: guice/AssistedInject). Dagger assisted injection. To ...
Read more >
How to produce the SavedStateHandle in your ViewModel ...
... a generic ViewModel Factory. (you can check the implementation in the article provided above). So, what does AssistedInject do to help ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found