injectable icon indicating copy to clipboard operation
injectable copied to clipboard

Restricted Generic Type (T Extends SomeClass) Problem

Open Peng-Qian opened this issue 5 years ago • 2 comments

Problem

when generating the restricted generic type as follow, the dynamic will filled in and result in exceptions

Annotation

@injectable
class Repository<T extends Entity> {
  ...
}

Generated code

  gh.factory<Repository<dynamic>>(() => Repository<dynamic>());

which will not compile...

Peng-Qian avatar Sep 12 '20 10:09 Peng-Qian

@Peng-Qian I see. This should normally register without a type? how would you manually register it?

Milad-Akarie avatar Oct 10 '20 18:10 Milad-Akarie

I think the problem here is that you cannot know what T is when registering and get_it does not provide a way to know either. IMO the best workaround for this is to register a factory class instead.

@injectable
class RepositoryFactory {
    Repository<T> getRepositoryFor<T>() => Repository<T>();
}

Which you can then use as GetIt.instance.get<RepositoryFactory>.getRepositoryFor<MyEntity>()

glassp avatar Jun 29 '22 17:06 glassp

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions

github-actions[bot] avatar Feb 21 '23 08:02 github-actions[bot]